How to use a Java for loop in an html page - java

In my system i need to use a java for loop inside an html table to print time from 8.00-17.00. Can someone please explain me how to do it.Thank you.
Html file
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<link rel="stylesheet" type="text/css" href="static/css/timeTable.css" th:href="#{/css/timeTable.css}">
<meta charset="UTF-8">
<title>Time Table</title>
</head>
<body>
<div class="container" style="margin-top:30px">
<br><br>
<table border="1" class="table2">
<thead>
<tr>
</tr>
<br>
<tr>
<td></td>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
</tr>
</thead>
<tbody>
<tr>
<% for(int i=0;i<17;i++){
<td> i.":00 - ".($i+1).":00" </td>
}
</tr>
</tbody>
</table>
</div>
</body>
</html>

You should install JSTL as follows:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
and then iterate over the list as follows:
<c:forEach var = "i" begin = "8.00" end = "17.00">
Item <c:out value = "${i}"/><p>
</c:forEach>

I was able to do it by using the following way. Thx everyone who tried to help
<option th:each="i : ${#numbers.sequence( 8, 17)}">
<tr>
<th th:text="${ i }+':00 - ' + ${ i+1 }+':00'"></th>
</tr>
</option>

Why don't you use Javascript instead of going nut with inventing stuff like that?
Javascript is a powerfull language and have a for loop as well.
let to the front which belongs to the front.

Related

Looping over object array and using c:if jstl in a JSP

I am currently working on a small web app project. The homepage has a link that, once clicked, calls a servlet that retrieves data from a database (in the form of an arraylist of objects), adds arraylist to session(as customers), and redirects to a jsp where the objects (customers) are displayed in a table. There is a "more details" column where each row has a button which submits a form, to another jsp, with a hidden input value with customerNumber as its value and num as its name.
I want, in the second jsp, to loop over all customers and only display the one whose customerNumber matches the one sent from the first jsp.
Current code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<% String hidden = request.getParameter("num"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Details Page</title>
</head>
<body>
<div>
<c:forEach var="c" items="${customers}">
<c:if test = "${hidden == c.customerNumber}">
<table>
<thead>
<tr>
<th>Customer Phone</th><th>Customer Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>${c.phone}</td><td>${c.country}</td>
</tr>
</tbody>
</table>
</c:if>
</c:forEach>
</div>
</body>
</html>
The above code only prints the table heading. Any help appreciated.
Found the issue, was trying to access variable from scriplet directly from jstl tags. The following code works fine:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String hidden = request.getParameter("num");
pageContext.setAttribute("reqNum",hidden);
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Customer Details Page</title>
</head>
<body>
<div>
<c:forEach var="c" items="${customers}">
<c:if test = "${reqNum == c.customerNumber}">
<table border="1">
<thead>
<tr>
<th>Customer Phone</th><th>Customer Country</th>
</tr>
</thead>
<tbody>
<tr>
<td><c:out value="${c.phone}" /></td><td><c:out value="${c.country}" /></td>
</tr>
</tbody>
</table>
</c:if>
</c:forEach>
</div>
</body>
</html>
Instead of using
<td>${c.phone}</td><td>${c.country}</td>
You can try:
<td><c:out value="${c.phone}" /></td><td><c:out value="${c.country}" /></td>

How to store the value entered dynamically in a form made in jsp into arraylist?

On clicking the button add organisation, a form gets displayed with parameters ID and NAME. After inputting the ID and NAME when the user clicks the submit button, the given entry should get stored and when the main table of organisation is viewed in home page it should have the values entered by the user. You can make use of anything MVC JSP JAVA or anything.
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Organization List</title>
</head>
<body>
<table border= '1' class="floatedTable">
<thead>
<tr>
<th>Organization ID</th>
<th>Organization Name</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>Tieto</td>
</tr>
<tr>
<td>2</td>
<td>Microsoft</td>
</tr>
<tr>
<td>3</td>
<td>Google</td>
</tr>
<tr>
<td>4</td>
<td>Chevron</td>
</tr>
</table>
<br>
<table border= '1' class="floatedTable">
<thead>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Address</th>
<th>Employee Works in</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>Dheeraj Kumar</td>
<td>Pune</td>
<td>Tieto</td>
</tr>
<tr>
<td>2</td>
<td>Revan</td>
<td>Wagholi</td>
<td>Microsoft</td>
</tr>
<tr>
<td>3</td>
<td>Deepali</td>
<td>karvenagar</td>
<td>Google</td>
</tr>
<tr>
<td>4</td>
<td>Amol</td>
<td>Bavdan</td>
<td>Chevron</td>
</tr>
</table>
<br>
<button type="button" onclick="location = 'AddOrganisation.jsp'">ADD Organization</button>
<button type="button" onclick="location = 'AddEmployee.jsp'">ADD Employee</button>
</body>
</html>
You can use a typical Spring MVC like
Spring Controller method:
#RequestMapping(value = "/organizations", method = RequestMethod.POST)
public String save(#ModelAttribute("organizationForm") #Validated User user,
BindingResult result, Model model,
final RedirectAttributes redirectAttributes)
Spring Form in jsp :
<form:form method="post" modelAttribute="organizationForm" action="/organizations">
<form:input path="name" type="text" />
Use Model attribute to map your input dynamically formed from jsp to Controller and store the result into same object so that it can be viewed in Home Page.
you will need SPring libraries like
org.springframework.asm-x.y.z.jar
org.springframework.beans-x.y.z.jar
org.springframework.context-x.y.z.jar
org.springframework.core-x.y.z.jar
org.springframework.expression-x.y.z.jar
org.springframework.web.servlet-x.y.z.jar
org.springframework.web-x.y.z.jar
spring-web.jar
Refer any Spring MVC with Model attribute implementation that will serve your purpose.

Restoring Table row data after refreshing the page

I am having task that i have to move the selected row to top in table and when i refresh the page i want to restore the previous changes.
I completed the moveRow logic.
But i am not getting how to restore the row data, please assist me for this.
Below is my code
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script language="JavaScript">
var whichrow = false;
var TableLocation;
function thisrow(x)
{
TableLocation = x.sectionRowIndex;
}
function MoveUp()
{
var tablebody = document.getElementById('table1');
if(TableLocation > 0)
{
tablebody.moveRow(TableLocation, 1);
}
}
</script>
</head>
<body>
<form>
<table id="table1" name="table1" border="1">
<tr>
<th>Name</th>
<th>Address</th>
<th>Select</th>
</tr>
<tr id="tr3" onclick="thisrow(this)">
<td><input type="text" name="name"></td>
<td><input type="text" name="address"></td>
<td><input type="checkbox" name="checkbox1"></td>
</tr>
<tr id="tr4" onclick="thisrow(this)">
<td><input type="text" name="name"></td>
<td><input type="text" name="address"></td>
<td><input type="checkbox" name="checkbox1"></td>
</tr >
<tr id="tr5" onclick="thisrow(this)">
<td><input type="text" name="name"></td>
<td><input type="text" name="address"></td>
<td ><input type="checkbox" name="checkbox1"></td>
</tr>
</table>
<input type="button" value="move row up" onClick="MoveUp();">
</form>
</body>
</html>
Thanx in advance...
looking forword for your reply.
You need to save your changes on the server to reload your page (you don't need to save on database, you can store it in the session and use use on the next page rendering). Then you can use your javascript to move up/down your rows.
I suggest you to put data-attributes in your html tags to order it in javascript or order on the server before rendering.

struts resource bundle properties file not mapping some keys

I'm starting to study struts and I have a problem using resource properties file
some text on page is displayed as this:
???login.message???
???login.username???
???login.password???
but some other messages are correctly taken from properties file. I think that the propertis file is correctly configured but I'm missing something to display anything correctly.
the file ApplicationResources.properties
# Resources for Login Project
# Struts Validator Error Messages
# These two resources are used by Struts HTML tag library
# to format messages. In this case we make sure that errors
# are red so that they can be noticed.
errors.header=<font color="red">*
errors.footer=</font>
#errors associated with the Login page
error.username.required=username required.
error.password.required=password required
error.login.invalid=The system could not verify your username or password. Is your CAPS LOCK on? Please try again.
#login page text
login.title=this is a title
login.message=please log in
login.username=username:
login.password=password:
login.button.signon=Log In
#loggedin page text
loggedin.title=Login Project
loggedin.msg=Benvenuto, {0}. You are now logged in.
"error.login.invalid" is correctly displayed and "error.username.required" too
the login label not
this is my jsp page
<%# 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">
<%# taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<%# taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html locale="true"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<fmt:bundle basename="ApplicationResources"/>
<title><fmt:message key="login.title"/></title>
</head>
<body>
<html:errors property="login"/>
<html:form action="login.do" focus="userName" >
<table align="center">
<tr align="center">
<td><H1><fmt:message key="login.message"/></H1></td>
</tr>
<tr align="center">
<td>
<table align="center">
<tr>
<td align="right">
<fmt:message key="login.username"/>
</td>
<td align="left">
<html:text property="userName"
size="15"
maxlength="15" />
<html:errors property="userName" />
</td>
</tr>
<tr>
<td align="right">
<fmt:message key="login.password"/>
</td>
<td align="left">
<html:password property="password"
size="15"
maxlength="15"
redisplay="false"/>
<html:errors property="password" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<html:submit>
<fmt:message key="login.button.signon"/>
</html:submit>
</td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
</body>
</html>
Can you help me ?
tkz
Your
<fmt:message ... />
tags need to be inside an
<fmt:bundle ... >
tag. Currently you are closing your bundle tag right away
<fmt:bundle basename="ApplicationResources"/>
Instead, open it
<fmt:bundle basename="ApplicationResources">
and close it
</fmt:bundle>
when you no longer need it, possibly at the end of your JSP. Nest your
<fmt:message key="login.title"/>
tags inside it.

How can I filter a table displayed using Expression Language in JSP?

Here's my code that I am using to display data from the database.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%# page import="java.io.*,java.util.*,java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Book List</title>
</head>
<body>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/BookTracker" user="root" password="school" />
<sql:query dataSource="${snapshot}" var="result">
SELECT * from BookTrackerSystem;
</sql:query>
<table border="1" width="100%">
<tr>
<th>Book ID</th>
<th>Book Name</th>
<th>Book Author</th>
<th>Book Genre</th>
<th>Book Description</th>
<th>Book Due Date</th>
<th>Book Status</th>
<th>Full Name</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}" /></td>
<td><c:out value="${row.bookName}" /></td>
<td><c:out value="${row.bookAuthor}" /></td>
<td><c:out value="${row.bookGenres}" /></td>
<td><c:out value="${row.bookDesc}" /></td>
<td><c:out value="${row.bookDueDate}" /></td>
<td><c:out value="${row.bookStatus}" /></td>
<td><c:out value="${row.fullname}" /></td>
</tr>
</c:forEach>
</table>
</body>
</html>
What I want to do, using a simple textbox I want to be able to filter the table displayed using the code above, I believe I can do it using jQuery but is jQuery compatible with Expression language or is there any other method I can make use of?
Despite the fact almost nobody will support you in what you are doing :) .. I mean all is in JSP..
The exact answer is the following:
Add an html form with a textbox.
Add code that reads the textbox value as request.getParameter().
http://www.tutorialspoint.com/jsp/jsp_form_processing.htm
Add the value from (2) into your query <sql:param value="..." /> and add where clause. Perhaps, you will need IF for two cases: a) filtering off - no text value and no where b) filtering on - text value exists and where used.
This will be a server-side processing (filtering) though.
If you want client-side filtering consider using DataTables http://www.datatables.net/

Categories