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

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>

Related

How to use a Java for loop in an html page

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.

How to dynamically change HTML input name

I'm creating a small shopping cart project where I get a list of products in a linked list from a servlet. Now, I am printing the values of the linkedlist in a table with an option for users to select the quantities they want.
Which ever has quantity selected should go on to the next page as an attribute
Here are few issues I am facing:
1: Since the list has more than 1 items, each item listed should have
input field. How to I dynamically change the input name for the
quantity which I can later use as an attribute.
2: The available quantity of items vary from product to product, how
to I set the max value of the quantity to the available stock.
3: If I can manage to get the values, how do I set all the attributes.
Is it in the for loop or outside the for loop?
Here is the code for the reference.Picture of what the page looks like
<%# 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>
</head>
<body>
<%-- <%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %> --%>
<%#page import="shoppingcart.model.items.*,java.util.*" %>
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity(); %>
<td><input type="number" name="should dynamically change according to the size of the list" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>
Try this:
<td><input type="number" name="quantity<%=i%>" min="0" max="<%=listp.get(i).getQuantity()%>"></td>
and you need to set all the attributes outside the loop.
Edit your code like this
<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");
%>
<%-- <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>
<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%
String name;
for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity();
name="quantity".concat(listp.get(i).getQuantity());
%>
<td><input type="number"name="<%=name%>" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>
</div>
</body>
</html>

SpringMVC - request syntactically incorrect

I have the following page (Spring NVC) which works fine:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Replace Cards</title>
</head>
<body>
<h3>Replace Cards</h3>
<br/><br/>
Give the numbers of cards in your hand you want replace and hit the button<br/>
Valid numbers: 1 to 5, numbers must be separated by space<br/>
#NEW CODE#
<br/>
<sf:form action="execReplaceCards" commandName="cards">
<table>
<tr>
<td><sf:input path="playerName" type="text"/></td>
<td><sf:input path="numbers" type="text"/></td>
<td><input value="Replace" type="submit"/></td>
</tr>
</table>
</sf:form>
</body>
</html>
until I add the following piece of code before the form tag
(marked above as #NEW CODE#):
Here is your hand:
<br/><br/>
<c:out value="Hand name: "/>
<c:out value="${hand.name}"/><br/>
<c:forEach var="card" items="${hand.cards.card}">
<c:out value="Card:[${card.suit} : ${card.rank}]"/><br/>
</c:forEach>
<c:out value="${hand.value}"/>
<br/>
This snippet doesn't do anything except displaying the players hand for reference but since I add this part I've got request syntactically incorrect error. Request never reaches the controller. What's wrong, why is it happening?

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