I am trying use JSTL on an old project where I have to use nested forEach.
My requirement is for example to iterate through a countries list, where each country will have a StateList which will contain State details.
<c:forEach items="${countryList}" var="country">
<c:out value="${country.name}" escapeXml="true" />
<c:forEach items="${country.StateList}" var="state">
<c:out value="${state.name}" escapeXml="true" />
</c:forEach>
</c:forEach>
When I try these I am getting error.
An error occurred while evaluating custom action attribute "items" with value "${country.StateList}": Unable to find a value for "StateList" in object of class "com.pack.Country" using operator "." (null)
What I am doing wrong? Is there a alternate method to do the same without using scriplets?
Issue Solved : The problem was that i used StateList instead of stateList as list name in class. Thanks.
Related
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 been googling and found that I can remove a session attribute using:
<c:remove var="foo" />
What I want is to clear all session attributes from a JSP, something like this:
<c:forEach var="item" items="${sessionScope}">
<c:remove var="${item }" scope="session"/>
</c:forEach>
The problem is that the code from above gives me this warning
c:remove doesn't support runtime expression
And I can't view the JSP where I put the code.
Is it possible? Is it a good practice to do something like this?
Because your are iterating through "item" and while iterating it inside loop you are trying to delete.
Better do this logic in java only rather than 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}.
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 want to understrand what happens when i use JSTL to access maps,in Hidden features of JSP/Servlet
in #blausC's answer, he explained what happend, but when i try to use the following code
<c:set var="resultMap" value="validationResults" scope="request"></c:set>
<c:if test="${resultMap['userName'] != null}">
${resultMap['userName'].details}
</c:if>
a confused exception happend
Caused by: javax.el.PropertyNotFoundException: Property 'userName' not found on type java.lang.String
The key of the map should be string, so whay is this exception, i tried the examples in the above question and the same exception,Can some one tell me where I have misunderstanding?
Edit: I populate the map in the servlet and send it to jsp
Map<String, ValidationResult> result = new HashMap<String, ValidationResult>();
aValidationResult = new ValidationResult();
check whether the field is valid or not if not fill the map
result.put("userName", aValidationResult);
result.put("group", aValidationResult);
if map is not empty, return the map to jsp
request.setAttribute("validationResults", result);
the map is filled when i make server side validation ,
Thanx in advance.
resultMap is a String because of this line
<c:set var="resultMap" value="validationResults" scope="request"></c:set>
You need to use EL to assign the value
<c:set var="resultMap" value="${validationResults}" scope="request"></c:set>
Edit: The following is working code
<c:set var="validationResults" value="<%= new java.util.HashMap() %>" />
<c:set target="${validationResults}" property="username" value="Hello World" />
<c:set var="resultMap" value="${validationResults}" />
<c:out value="${resultMap['username']}"></c:out>
This is caused because String class does not have a method called getUserName()