JSTL - Assign list content into an array - java

I have list of users and I want to assign each user into an index of array:
<%
User users[] = new User[n];
pageContext.setAttribute("users", users);
%>
Now:
<c:forEach items="${usersList}" var="user">
// here I want to assign each user into an array index like:
// users[index] = user
</c:forEach>
At the end I want to access each index of array manually. Something like below:
<p><c:out value="${users[0].getName()}" /></p>
I know above is not correct, can some one help how can I achieve this via jstl? Thanks.
P.S: Please be noticed that I want to access the array by index manually.

You can use varStatus to get index in the loop
<c:forEach items="${usersList}" var="user" varStatus="status">
Index: ${status.index}
</c:forEach>

Related

Iterate a Set inside a List using JSTL

I need iterate a Set what is into a List using JSTL.
When I try to iterate the set, I get the following error:
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "codis"
What I'm trying:
<c:forEach var="game" items="${games}">
<a href="<c:url value="/product?id=${game.name}"/>"id="${game.id}">
<c:set var="codi" value="${game.codes}"></c:set>
</c:forEach>
The "codes" are supposed to be the SET but i get the above error.
Please try below code. Iterating over a Set or List has no difference in JSTL
<c:forEach var="game" items="${games}">
<a href="<c:url value="/product?id=${game.name}"/>"id="${game.id}">
<c:forEach var="codi" items="${game.codes}">
<c:out value="${codi}"/>
</c:forEach>
</c:forEach>
Tag <c:set> is used to set value to some variable, while your case is to iterate the set and display its content.

Access list of list in JSP file

I have created a list of lists that contains content that I want to display through a jsp file. when trying to just display the items through one list, the file works and I see it. But when I split the items in different arraylists and try to iterate over that, nothing shows up. My initialization is,
private final List<ArrayList<DisplayableProduct>> listOfThreeProducts = new ArrayList<ArrayList<DisplayableProduct>>();
I have verified that there is content inside each list of it through debugging.
my model.listofThreePorducts is a list of lists. so I want to loop through the list of lists and then loop inside each loop and so stuff. Is it correct to pass the var="listoflists" value to the second for loop as such below? would it be items="${listoflists}" to access everything in that list ?
<c:forEach items="${model.listOfThreeProducts}" var="listoflists">
<div id="hero-featureSwap">
<c:forEach items="${listoflists}" var="product">
<div class="widget-element-brand"
title='<awsmp:formatText text="${product.vendorName}" />'>
<awsmp:formatText text="${product.vendorName}" maxLength="25" />
</div>
</c:forEach>
</div>
</c:forEach>
You need standard getter in model object for property listOfThreeProducts
further detail discussed in question's comment section

Having exception in accessing a list through JSTL

I'm trying to access to a like this:
I pass to the JSP page
the list through request.setAttribute("list", list);
and try to access
<c:foreach items="${list}" var="element"}>
<li> ${element.name} ${element.price} </li>
</c:foreach>
but I get NumberFormatException. How can I access correctly the list?
If you select only a few columns from a table, JPA will return an array of objects for each row returned. i.e. it will return a List<Object[]> object. If you want to get back a list of Route objects you can write a constructor in the Route class that takes two values(name and pric and set the values appropriately in the constructor. You can then use the constructor in the JPA query like below to get Route objects:
select new yourpackage.Route(name, price) from Route
There are two issues in your JSTL:
<c:foreach items="${list}" var="element"}>
...
</c:foreach>
Its c:forEach not c:foreach.
There is one extra } in the end.
It should be like this:
<c:forEach items="${list}" var="element">
...
</c:forEach>
There are two option. Try any one as per need.
If the list contains Object[] then use ${element[0]}
If the list contains Route then use ${element['name']} or ${element.name} or ${element.getName()}. Make sure Route class contains name as instance variable with getter & setter methods.

passing string array as hidden value from one jsp to another jsp

I am trying to pass String array from one jsp to another. I am using JSTL in my JSP.
In my first JSP i am doing like this
<c:if test="${fn:length(empBean.additionalEmailAddr) gt 0}">
<c:forEach begin="0" end="${fn:length(empBean.additionalEmailAddr) - 1}" var="ind" >
<input type="hidden" name="inbdAdditionalEmailAddr" value="${empBean.additionalEmailAddr[ind]}"/>
</c:forEach>
</c:if>
and trying to access the values in another jsp as follows
<%
String[] inbdAddEmlAddr = request.getParameter("inbdAdditionalEmailAddr");
%>
and i am planning to use JSTL to print the array values.
In the second jsp i am getting type mismatch error. Please help.
Is this the right approach ? Any help is appreciated
Thanks
request.getParameter() returns a String which the code attempts to assign to a String[], causing the exception.
Use request.getParameterValues('inbdAdditionalEmailAddr'); to retrieve parameters as an array.
See the documentation.

How to iterate the jstl loop by using integervalues

Hi here I am using jstl to loop over the content I need to convert the number in status1.noOfPages into integer and i want to use this integer in the begin value of next loop.....could anybody plz help me out....
<c:forEach var="status1" items="${list1}">
<c:set var="wins" ><fmt:parseNumber type="number" value="${status1.noOfPages}" /></c:set>
<c:forEach begin="0" end="wins" varStatus="loop">
Index: ${status1.noOfPages}<br/>
</c:forEach>
</c:forEach>
<fmt:parseNumber type="number" value="${status1.noOfPages}" var="beginningIndex"/>
<c:forEach begin="${beginningIndex}" ...
But you shouldn't have to parse anything in a JSP. Why isn't status.noOfPages an int to begin with? Or why don't you parse it in the controller, and provide the parsed value to the JSP?

Categories