How to iterate the jstl loop by using integervalues - java

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?

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.

How do I use an object for setting variable in JSP for loop?

I need to loop through the list, but foreach loop is not acceptable as I want to start from the second element.
<c:forEach var="item" items="${items}">
<c:out value="${item.name}"/>
</c:forEach>
I tried to use standard java code in "<% %>" but I cannot come up how to set it up right. My syntax is just wrong, I need something like this
<% for (int i = 1; i < ${items.size()}; i++) { %>
<c:out value="${item.name}"/>
<%} %>
But, obviously, the code above is not working.
You can use JSTL varStatus property. It might help you.
<c:forEach var="item" items="${items}" varStatus="loop">
<c:if test="${loop.index != 0}">
<c:out value="${item.name}"/>
</c:if>
</c:forEach>

how to iterate using jstl code

This is my code
<c:set var="radioCount" scope="session" value="${radiocount}"/>
I want to create a table with no of rows equal to radiocount so trying to use the below expression but getting an error
<c:forEach begin='1' end='radioCount'>
<tr>
<td>
<input style='width:500px' type='text' / >
</td>
</tr>
</c:forEach>
Try to use bracktes in your forEach and use double quote " instead of ' in your code:
<c:forEach begin="1" end="${radioCount}">

Manipulating Hashtable key is not working

I am trying to access a Hashtables value based on its key, which is a number as String in JSTL.
But if I increment/decrement the keys value, it does not work anymore.
I iterate the sorted list of keys in a for loop. I use this item to access Hashtable.
<c:forEach items="${helper:getSortedList(hashtableObj)}" var="lineNumber" varStatus="loop">
<c:if test="${param.lineNbr eq lineNumber}">
<c:if test="${lineNumber>1}">
<fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
<c:out value="PREV ${hashtableObj[prevLineNumberKey]}" escapeXml="false"/><br/>
</c:if>
<c:out value="Current :${lineNumber}" /><br/>
<c:if test="${lineNumber<fn:length(hashtableObj)-1}">
<fmt:parseNumber var="nextLineNumberKey" type="number" value="${lineNumber+1}" />
<c:out value="NEXT ${hashtableObj[nextLineNumberKey+1]}" escapeXml="false"/><br/>
</c:if>
</c:if>
</c:forEach>
The output is
PREV Current :51 NEXT
But what I expected is
PREV 50 Current :51 NEXT 52
Any pointers are appreciated.
If keys in your Map is String than to get an element you must query it with String value. Your current solution queries a Map with Long value.
You can convert number to String and then query a Map like this:
<c:set var="numberAsString">${50 - 1}</c:set>
<c:out value="value: ${hashtableObj[numberAsString]}"/>
Try replacing:
<fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
With:
<c:set var="prevLineNumberKey">${lineNumber-1}</c:set>
And replace:
<fmt:parseNumber var="nextLineNumberKey" type="number" value="${lineNumber+1}" />
<c:out value="NEXT ${hashtableObj[nextLineNumberKey+1]}" escapeXml="false"/><br/>
With:
<c:set var="nextLineNumberKey">${lineNumber+1}</c:set>
<c:out value="NEXT ${hashtableObj[nextLineNumberKey]}" escapeXml="false"/><br/>
Couple of questions though:
1) Is hashtableObj really a hashtable or is it a hashmap?
2) Is the value of the hashtableObj, really a number that is equal to the key? In other words you are expecting:
PREV 50
... that means you are expecting the value of the hashtable/map to be 50 AND the key is also 50?
I found a workaround.
<fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
<c:out value="Previous ${hashtableObj[sortedList[prevLineNumberKey-1]]}" escapeXml="false"/><br/>
I used the list element as a key for Hashtable and it works. Thanks to all answers.

How to use the index variable of a JSTL forEach loop to access a map entry?

With a forEach loop I'd like to create table cells (for a row) whereas each cell contains an input field of a form. The number of table cells is always fixed (12). That is actually no problem. However, here comes the challenge: the forEach should also enter a variable number of default values into the input fields that have to be obtained from a Map(Long, Double).
This is my (simplified) attempt:
<c:forEach var="number" begin="1" end="12" >
<td>
<input type="text" value="${requestScope.aMapWithData[number]}" />
</td>
</c:forEach>
But this doesn't show any value from the Map in the input fields. I guess the problem is that "number" is of type String and not Long. So I wonder if this problem can be solved without using scriptlets.
What number do you want to show? Is it index number of each map entry?
<c:forEach items="${aMapWithData}" var="item" varStatus="status">
<td>
<c:out value="${status.count}."/>
<input type="text" name="${item.key}" value="${item.value}" />
</td>
</c:forEach>
Try this
<c:forEach items="${aMapWithData}" var="mapEntry">
<c:set var="mapKey" value="${mapEntry.key}"></c:set>
<c:set var="mapValue" value="${mapEntry.value}"></c:set>
</c:forEach>

Categories