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}.
Related
How can I use the Jstl attribute ${theDetail.id} from a Jstl foreach inside a java function? I have tried a lot, but nothing works.
<c:forEach items="<%= facade.getAllDetails() %>" var="theDetail">
// how to use ${theDetail.id} inside this java function
<c:forEach items="<%= facade.getSomeStuffById(...) %>" vars="theStuff">
${theStuf.name}
</c:forEach>
</c:forEach>
Never use scriptlet expressions inside JSP tags. In fact, never use scriptlets at all. The JSP tags are designed to use the JSP EL expressions. Not scriptlet expressions.
The way to write your code is, assuming facade is an attribute of some scope
<c:forEach items="${facade.allDetails}" var="theDetail">
<c:forEach items="${facade.getSomeStuffById('someHardCodedId')}" var="theStuff">
${theStuff.name}
</c:forEach>
</c:forEach>
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.
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.
Similar problems are invoked in many posts in this forum; but no one has a solution that specific one, I thank you for helping me in this :
I'm using spring to develop a web application,
I don't know what I should put in the path of the form:checkbox tag which inside the c:foreach one, here is my code :
<c:forEach items="${persons}" var="person" varStatus="i">
<tr>
<td><c:out value="${person.firstName}" /></td>
<td><c:out value="${person.lastName}" /></td>
<td><form:checkbox path="person.rights" value="Download"/>Download </td>
<td><form:checkbox path="person.rights" value="Delete"/>Delete </td>
</tr>
</c:forEach>
'rights' is a list of Strings as it defined in the spring documentation, it has a getter and a setter like the other properties, my checkboxes work outside the c:foreach tag, but when including them into this tag this exception is generated :
org.springframework.beans.NotReadablePropertyException: Invalid property 'person' of bean class [java.util.ArrayList]: Bean property 'person' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
do you have an idea about what the problem is ??
This problem is strangely undocumented on most places. Here is an extract from the links I am posting below. The gist is that we need a static placeholder which maps to the type instead of the value of the bean. So anything inside a ${} will not work out. For this, and in the specific case of using a JSTL loop operator <c:forEach> with s[ring form tld, we should refer to the type information in each iteration using the varStatus attribute of the <c:forEach> operator, just like indices of an array, and thus refer to the inner properties of the iterable collection using . on the collection variable accessible via the outermost bean backing up the form.
For example:
<c:forEach items="${teamslist_session.teams}" var="team" varStatus="teamsLoop">
<form:input path="teams[${teamsLoop.index}].name"/>
</c:forEach>
where:
teamList_session is the bean backing up the form
teams is the collection of beans the properties of which we need to set in the path attribute
var is the a reference to each member of the teams collection
teamsLoop is the iteration index, which is used in the line below to refer to the say, ith element's bean's property called name
Please refer to the following links for more information:
Forum Discussion - See the last post
The link provided for reference in link 1
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>