Using template in Underscore JAVA - java

How to use underscore java template library for using template in java.
Library : https://github.com/javadev/underscore-java
Sample Template:
<h2>
<%- listTitle %>
</h2>
<ul>
<% _.each( listItems, function( listItem ){ %>
<li>
<%- listItem.name %>
<% if ( listItem.hasOlympicGold ){ %>
<em>*</em>
<% } %>
</li>
<% }); %>
</ul>
<% var showFootnote = _.any(
_.pluck( listItems, "hasOlympicGold" )
); %>
<% if ( showFootnote ){ %>
<p style="font-size: 12px ;">
<em>* Olympic gold medalist</em>
</p>
<% } %>
How can populate this template in java.
Thanks.

Related

How to refresh only a division of jsp page

I am developing a web application and I have a jsp page in which I have a table and few other things.All I want is to refresh the contents of table every 5 seconds.Below is my code of jsp page.Can anyone help me solve my problem.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page language="java" %>
<%# page import="java.sql.*" %>
<%# page import="java.util.Calendar" %>
<%# page import="java.text.DateFormat" %>
<%# page import="java.util.Date" %>
<%# page import="java.text.SimpleDateFormat" %>
<HTML>
<HEAD>
<TITLE>Welcome to Crevavi </TITLE>
</HEAD>
<div style="width:950px; height:900; padding:10px; border:10px ridge black;">
<body bgcolor="white"; border="3px">
<img src="Crevavi_Plain.jpg" background-color="white" width="100" height="25" style=float:right;/>
<h1 style=margin-left:2px;><font size="5"> Crevavi Web Application</font></h1>
<hr color="black">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var auto = setInterval( function ()
{
$("#result").load("NewTable.html #result");
}, 5000); // refresh every 5000 milliseconds
</script>
//I want only the below division to refresh every 5 seconds.
<div id="result" style="width:930px; height:500; padding:5px; border:5px ridge black;">
<%
int rowCount = 0;
/*Calendar cal = Calendar.getInstance();
Date date1=cal.getTime();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy,HH:mm:ss");
String formattedDate=dateFormat.format(date1);
System.out.println("Current time of the day using Calendar - 24 hour format: "+ formattedDate);
String[] values = formattedDate.split(",");
String date = values[0];
String time = values[1];*/
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/students","root","root");
Statement statement = connection.createStatement() ;
/* String sql = "INSERT INTO TEXTDATA (MachID,Date,Time,Text1,Text2,Text3,Text4,Text5,Text6,Text7,Text8,Text9,Text10,Text11,Text12) VALUES ('123','"+date+"','"+time+"','My','hello','thankyou','welcome','visit again','haha','good morning','sweet dreams','hi','hello','night','work')";
((java.sql.Statement) statement).executeUpdate(sql);*/
ResultSet resultset = statement.executeQuery("select * from textdata order by Date desc, Time desc");
while(resultset.next()){
rowCount++;
}
int firstrow = rowCount-10;
System.out.println(firstrow);
if(rowCount > 10){
resultset = statement.executeQuery("select * from textdata where Rowcount>'"+firstrow+"' order by Date desc, Time desc");
}else{
resultset = statement.executeQuery("select * from textdata order by Date desc, Time desc");
}
%>
<TABLE BORDER="1">
<TR>
<TH>Mach ID</TH>
<TH>Date</TH>
<TH>Time</TH>
<TH>Text1</TH>
<TH>Text2</TH>
<TH>Text3</TH>
<TH>Text4</TH>
<TH>Text5</TH>
<TH>Text6</TH>
<TH>Text7</TH>
<TH>Text8</TH>
<TH>Text9</TH>
<TH>Text10</TH>
<TH>Text11</TH>
<TH>Text12</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD> <%= resultset.getInt(1) %></td>
<TD> <%= resultset.getString(2) %></TD>
<TD> <%= resultset.getString(3) %></TD>
<TD> <%= resultset.getString(4) %></TD>
<TD> <%= resultset.getString(5) %></TD>
<TD> <%= resultset.getString(6) %></TD>
<TD> <%= resultset.getString(7) %></TD>
<TD> <%= resultset.getString(8) %></TD>
<TD> <%= resultset.getString(9) %></TD>
<TD> <%= resultset.getString(10) %></TD>
<TD> <%= resultset.getString(11) %></TD>
<TD> <%= resultset.getString(12) %></TD>
<TD> <%= resultset.getString(13) %></TD>
<TD> <%= resultset.getString(14) %></TD>
<TD> <%= resultset.getString(15) %></TD>
</TR>
<% } %>
</TABLE>
</div>
</br>
<form name = "Field_Details" action = "ServletApp" method= "get">
<fieldset style="float: center; width:900px; height: 75px;background-color:ivory; border-color:black;">
<font size = "2">Output Field :</font> <input type="text" name="Text1" maxlength="50" style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2"> MachId :</font> <input type="text" name="Text2" maxlength="15" style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2"> From Date(dd/mm/yy) :</font> <input type="text" name="Text3" maxlength="8" style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2"> To Date(dd/mm/yy) :</font> <input type="text" name="Text4" maxlength="8" style="height:15px; width:100px; border-color:black"><font size = "2"></font><br><br>
<input type= "submit" value="Send" style="height:30px; width:80px; margin-left:15px">
<input type= "submit" value="Search" style="height:30px; width:80px; margin-left:700px" onclick="form.action='FirstServlet';">
</BODY>
</HTML>
JSP is a server side technology, which means that if you want to refresh the page you will have to perform a request to the server which will return the new page. It is not possible to just return part of a page through normal JSP mechanisms.
If you want to just refresh the table you will need to use javascript to make an ajax call to the server to get the data you need, and repopulate the table with this data.
I altered code as below in my NewFile.jsp
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var auto = setInterval( function refresh()
{
$("#result").load("DemoFile.jsp");
}, 5000); // refresh every 5000 milliseconds
refresh();
</script>
then created new jsp file named DemoFile.jsp and copied part of code of NewFile.jsp division which I wanted to refresh.

Identifier expected after this token

I got a error : Identifier expected after this token
Syntax error on token "tag", Identifier expected after this token
And here is my code:
<%# page import="photoshare.Picture" %>
<%# page import="photoshare.PictureDao" %>
<%# page import="photoshare.HREF_AND_IMGSRC" %>
<%# page import="photoshare.ToolsDao" %>
<%# page import="org.apache.commons.fileupload.FileUploadException" %>
<%# page import="java.util.ArrayList" %>
<%# page import="java.util.List" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Get POP Tags</title></head>
<body>
<h1>With Photos with popular Tags:</h1>
<%
String tagName = request.getParameter("tag");
List<String> popTags = new ArrayList<String>();
ToolsDao toolsDao = new ToolsDao();
popTags = toolsDao.getPopularTags();
for (tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}
List<HREF_AND_IMGSRC> tagPhotos = toolsDao.getHrefAndImgSrcFromTag(tagName);
for (HREF_AND_IMGSRC photo:tagPhotos) {
int pictureId = photo.getPictureID();
int selectedAlbumID = photo.getAlbumID();
String selectedAlbum_id = Integer.toString(selectedAlbumID);
%>
<td><a href="picture.jsp?id=<%= pictureId %>&album_id=<%= selectedAlbum_id%>">
<img src="/photoshare/img?t=1&picture_id=<%= pictureId %>"/></a></td>
<%
}
%>
Click here to Main Page<br>
</body>
</html>
I am finding some reasons for this but nothing similar, can anybody help me to figure out what is the problem for this specified error?
I found the problem which is :
for (tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}
I have to declare the type of the tag in popTags list such as:
for (String tag:popTags) {%>
<a href="popTags.jsp?tag=<%= tagName %>">tagName <a>
<%}

How to make a youtube link as embed?

<%
String song = request.getParameter("songname");
String band = request.getParameter("band");
String url = request.getParameter("url");
%>
<div align="center">
<br><br> <% out.print(band + " - " + song); %><br><br>
<iframe width="560" height="315" src="<%out.print(url);%>" frameborder="0" allowfullscreen></iframe>
</div>
<%
%>
Let's say url i get is like this https://www.youtube.com/watch?v=kXYiU_JCYtU i want to make it look like https://www.youtube.com/embed/kXYiU_JCYtU this so i can put it at src=""
You can use the replace() method:
<% out.print(url.replace("watch?v=", "embed/")); %>

How to include HTML in JSP?

I've searched for this question, there are some answers, but not exactly as my question. So, here is my jsp code:
<body>
<%
if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
out.print("<h1> Welcome ");
out.print(request.getParameter("username") + "</h1>");
} else {
out.print("<h1> Please Try Again </h1> <br />");
out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
}
%>
<%# include file="LoginForm.html" %>
but instead of this, I want to include "loginForm.html" only in "else" block.
How can I do it?
Of course this doesn't work, but you'll guess what I want:
<%
if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
out.print("<h1> Welcome ");
out.print(request.getParameter("username") + "</h1>");
} else {
out.print("<h1> Please Try Again </h1> <br />");
out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
include file="LoginForm.html" ;
}
%>
I would like to show you a way, try like below. You can try with else instead with if.
<body>
<%
int x = 10;
if(x>10) {
%>
<%#include file="some.html" %>
<%
}
%>
</body>
You could also include reusable html in a jsp file function and call it for printing where needed. e.g.
config.jsp contains
<%!
public void printMenu()
{
%>
HTML HERE
<%!
}
%>
home.jsp contains
<%#include file="config.jsp"%>
<!DOCTYPE html>
<html>
<body>
<% printMenu(); %>
<div id="PeopleTableContainer" style="width: 800px;"></div>

How to set default value for drop-down/select in JSP?

I have an arraylist for Strings eqArray.
I need to show it in a drop-down list for which I'm using the following in my JSP:
<%
for(int count=0;count<eqArray.size();count++){ %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%}
%>
I have an eqName String which is part of eqArray and should be the selected value by default.
How do I go about it without having to check and set the first option as eqName always?
<% for(int count=0; count<eqArray.size(); count++){ %>
<option value="<%= eqArray.get(count) %>" <%= (eqArray.get(count).equals("eqName"))?"selected":"" %> ><%= eqArray.get(count) %></option>
<%} %>
Change the index of eqName element to 0 in the array, or use a conditional Statement.
<%
for(int count=0; count < eqArray.size(); count++){ %>
<%if(eqArray.equals("eqName"){ %>
<option selected="selected" value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%} %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%} %>
but use JSTL taglibs instead of using scriptlets.
You can do it via JQuery, which IMHO more clean:
<select data-selected="${eqName}">
<%
for(int count=0;count<eqArray.size();count++){ %>
<option value="<%=eqArray.get(count)%>"><%=eqArray.get(count)%></option>
<%}
%>
</select>
At the end of the page:
<script type="text/javascript">
$("select[data-selected]").each(function() {
var selected = $(this).data("selected");
$("select[data-selected='" + selected + "'] option[value='" + selected + "']").attr("selected", "selected");
})
</script>
By this way, you just need include the js at each of your page.
BTW, I recommend you to use JSTL and EL, which is more readable:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<select data-selected="${eqName}">
<c:forEach items="${eqArray}" var="model">
<option value="${model}">${model}</option>
</c:forEach>
</select>
You can implement these in two ways
By using jsp
" selected="selected" >
By Using Javascript at the end of the select box code
document.getElementById('selectBoxID').value=""
instead of selectBoxID id use the select box id

Categories