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.
Related
I want to dynamically generate model attribute name and use it in JSP.
For eq:
for (Integer integer : integers){
model.addAttribute("model_" + integer, integer);
}
model.setAttribute("integers",integers);
in jsp:
<c:foreach items=${integers} var=integer>
${model_integer} // Want to Print the value but throwing error.
</c:foreach>
This should work.
<c:foreach items=${integers} var=integer>
<c:set var="totalBuild" value="${0}"/>
<c:set var="totalBuild" value="${totalBuild + integer "/>
<c:set var="modelAtt" value="model_${totalBuild}" />
${modelAtt}
</c:foreach>
To print out the value you'll need to use the out tag
<!-- You need to surround the values of your attributes with quotes -->
<c:foreach items="${integers}" var="integer">
<c:out value="${integer}" /> <!-- the var name in the for each" -->
</c:foreach>
https://www.tutorialspoint.com/jsp/jstl_core_out_tag.htm
You should also add the integers to a list of some sort, then add the list as a model attribute.
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?
I have a HashMap in the controller:
HashMap<String, ArrayList<String> map = new HashMap<String, ArrayList<String>();
In the JSP page I want to access this through something like this:
<c:forEach var="list" items="${requestScope.list}">
<c:set var="testing" value="{requestScope.map}"></c:set>
<c:forEach var="anotherTesting" items="${testing['${list.item}']}">
<option><c:out value="${anotherTesting}"/></option>
</c:forEach>
</c:forEach>
Where list.item is a String but it is used for another process but I want it to be used to access the HashMap.
Is there a way to concatenate JSTL? Either map.key or map['key'] will do.
I guess simply this would work:
<c:forEach var="anotherTesting" items="${testing[list.item]}">
<option><c:out value="${anotherTesting}"/></option>
</c:forEach>
Notice the difference with and without quotes:
${testing[list.item]} is equivalent to testing.get(list.getItem());
${testing['list.item']} is equivalent to testing.get("list.item");.
Some Note:
You don't need to specify the scope to access the attributes, unless there is a conflict with the same name in different scopes. So, "${requestScope.list}" can be changed to ${list}, and "${requestScope.map}" can be changed to ${map}.
Please use a different name for var attribute of outer loop. May be listItem instead of list.
No need to set the map to a different variable. That <c:set...> is not needed. You can directly access the property of map attribute.
So, your loop can be modified to:
<c:forEach var="listItem" items="${list}">
<c:forEach var="anotherTesting" items="${map[listItem.item]}">
<option><c:out value="${anotherTesting}"/></option>
</c:forEach>
</c:forEach>
The code in ${...} is not JSTL but Expression Language. You don't need to c̶o̶n̶c̶a̶t̶e̶n̶a̶t̶e̶ nest EL ${} expressions, just add it cleanly.
Knowing this, the expression ${testing['${list.item}']} will be ${testing[list.item]}.
BUT note that this is not what you really want/need unless testing is indeed a Map<String, ArrayList<String>>, otherwise you will get unexpected results. From your code above, assuming requestScope.list is a List<Map<String, ArrayList<String>>>, then the code would be:
<c:forEach var="listItem" items="${list}">
<c:forEach var="innerString" items="${map[listItem.item]}">
<option><c:out value="${innerString}"/></option>
</c:forEach>
</c:forEach>
Note that ${list} is the same as ${requestScope.list} assuming there's no list attribute nor in page, session or application scope, similar for ${map}.
I have an forEach taglib inside another one. I would like to compare one attribute of each object being listed in its respective taglib.
<c:forEach items="${andares}" var="andar">
..........
<c:forEach items="${lojas}" var="loja">
<c:if test="${loja.andar == andar.numero}">
.....
</c:if>
</c:forEach>
</c:forEach>
I would like to only execute some code when the attribute "numero" of object andar is equals to attribute "andar" of object loja.
How can I do it?
I don't recall, but it seems like that should work. If not, perhaps this?
<c:forEach items="${andares}" var="andar">
<c:set var="andarval" value="${anar.numero}"/>
..........
<c:forEach items="${lojas}" var="loja">
<c:set var="lojaval" value="${loja.andar}"/>
<c:if test="${lojaval == andarval}">
....
</c:if>
</c:forEach>
</c:forEach>
I have a JSTL loop where I'm trying to check to see if a given variable is empty or not with a dynamic variable name. When I use c:set with page scope, the variable is not accessible to the if statement. However, when I set it using <% pageCotnext.setAttribute(...); %>, the variable is available.
<%
pageContext.setAttribute("alphaParA", "test");
pageContext.setAttribute("alphaParF", "test");
int i = 0;
%>
<ul class="alphadex_links">
<c:forEach var="i" begin="0" end="25" step="1" varStatus="status">
<c:set var="currentLetter" scope="page">&#${i+65}</c:set>
<c:set var="currentPar" scope="page">alphaPar${currentLetter}</c:set>
<% pageContext.setAttribute("currentPar", "alphaPar" + (char)('A' + i++)); %>
<li>
<c:choose>
<c:when test="${not empty pageScope[currentPar]}">
The test is always fails when I remove the pageContext.setAttribute block, however it succeeds for A and F as it should when the block is in. I'm very confused and can't find help anywhere.
It fails because HTML doesn't run at the moment JSTL runs. You're effectively passing a Java String A to it instead of the desired character A which would be represented as such based on the HTML entity A when the HTML is retrieved and parsed by the webbrowser after Java/JSP/JSTL has done its job. Please note that your HTML entity is missing the closing semicolon, but this isn't the cause of your concrete problem.
As to the concrete functional requirement, sorry, you're out of luck with EL. It doesn't support char. Your best bet is to deal with strings like this:
<c:forEach items="${fn:split('A,B,C,D,E,F,G,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', ',')}" var="currentLetter">
<c:set var="currentPar" value="alphaPar${currentLetter}" />
${pageScope[currentPar]}
</c:forEach>
If necessary, just autogenerate the letters as String[] in Java end and set it as application attribute.