Hello everyone,
I was just wondering whether it's possible to access all the stored cookies like this:
<c:forEach items="${cookie}" var="currentCookie" varStatus="lp">
<tr><td>${cookie[lp.index].key} </td></tr>
</c:forEach>
if not, is there any way I could somehow itterate over cookies and other array in one loop?
<c:forEach items="${cookie}" var="currentCookie" varStatus="lp">
<tr><td>${CurrentCookie.key} </td></tr>
<tr><td>${MyArray[lp.index].name} </td></tr>
</c:forEach>
Thanks for any suggestions,
Wrack
Yes you can interate over cookies using JSTL for each, its similar like iterating over a map.
Similar question Retrieving cookie and array values in JSTL tags
Related
I am displaying list of students with checkbox each to take attendance for each student. The problem is I am unable to justify the iteration for checkbox it asking items="${studentsList}" is mandatory presently my part of JSP page looks like this
.jsp
<c:if test="${!empty studentsList}">
<c:forEach items="${studentsList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.studentName}</td>
<td>${student.fatherName}</td>
<td><form:checkboxes path="studentsListFlag" items="${studentsList}"/></td>
</c:forEach>
</c:if>
If I set items="${studentsList}" in checkboxes iteration total list is getting displayed in single checkbox column, can anybody please suggest me how to iterate the above list. I am working hard for this your suggestions will help me a lot.
i have a question when i try to capture the value of dropdown list from jsp file to servlet.
<select name="extraroom">
<c:if test="${booking.roomType ne 'single'}">
<c:forEach var="i" begin="0" end="${booking.count}">
<option value="<c:out value="${i}"/>"><c:out value="${i}"/></option>
</c:forEach>
</c:if>
</select>
i use Integer.parseInt(request.getParameter("extraroom"))
i have many drop down lists according to different roomType.
but the result in java servlet always get the value of the first dropdown lists...
what should i change in servlet?
Thank you.
For a select to be retrieved by a server the select has to
be inside a form and
have a unique name identifier inside (it; best the identifier is unqiue across the entire DOM tree)
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'm new to using JSP and need to figure out how to do this. I'd appreciate any pointers on how to do this ?
I need to display images in this table-like structure. To simplify the problem,
A B C
D E F
G H I
where each of these elements are a part of the Set names in action class.
Set<String> names = new HashSet<String>(0);
names.add("A");
names.add("B");
names.add("C");
names.add("D");
names.add("E");
names.add("F");
names.add("G");
names.add("H");
names.add("I");
Its fairly trivial to do it in java, however, I am having a hard time to figure out, how do I ask the iterator to point to next element manually.
<s:iterator value="names">
<s:property/>
I'd now like to point iterator to point to next or run a nested iterator loop here.
</s:iterator>
You can use JSTL forEach loop. You can find a number of examples here.
You can easily accomplish it with JSTL:
<table>
<tr>
<c:forEach items="names" var="name" varStatus="i">
<c:if test="${!i.first && !i.last && i.index % 3 == 0}">
</tr>
<tr>
</c:if>
<td><c:out value="${name}" /></td>
</c:forEach>
</tr>
</table>
Doing so, a new line (</tr><tr>) will be added every 3 elements.
(not tested)
As CoolBeans said, you can use JSTL forEach loop.
If you'd like to see an example of that (alongside other good JSTL examples), check out JSTL Examples.
"Iterating over data structures" has some info and examples on what you're trying to do.
I have a couple of ArrayLists with variable length and sometimes null. This ArrayList contains a bunch of objects.
The table should have columns based on (some) attributes of the object. And the table should be displayed on a jsp.
I have two ideas, one is to use a JSTL tag the other is to use JavaScript. And library suggestions are welcome.
JSTL is the standard, preferred way (unless you need to load it via ajax, for example)
<table>
<tr><td>Foo header</td><td>Bar header</td></tr>
<c:forEach items="${yourRequestScopedArrayList}" var="obj">
<tr>
<td>${obj.foo}</td>
<td>${obj.bar}</td>
</tr>
</c:forEach>
</table>
JSTL is better,
Javascript you should avoid as much as possible ,
I am not sure how you are going to render datatable using java script and Collection
How to use jstl with collection that has been demonstrated by Bozho in the same thread.
Javascript doesn't have access to the Java objects that live (I presume) on the server. The server code can make the ArrayLists available to the JSP which can then loop over them with a JSTL forEach tag.
How you make the ArrayLists "available" depends on the framework you're using, but the plain servlet way is just setting an attribute from the doPost method.
request.setAttribute("list1", arrayList1);
The loop would be something like
<table>
<tr><th>Column 1</th> <th>Column 2</th> <th>Column 3</th></tr>
<c:forEach var="row" items="${list1}">
<tr><td>${row.col1data}</td> <td>${row.col2data}</td> <td>${row.col3data}</td></tr>
</c:forEach>
</table>