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.
Related
I need to show some data inside a table (jsp). The data are being passed like this:
request.setAttribute("name", nameVariable);
request.setAttribute("surname", surnameVariable);
request.setAttribute("list", list); //Here are stored ultimately all the data (so name and surname also)
My list is being updated and I need to have the list being updated also. I know my list gets more items, but this code prints only last record from that list. What should I change in my code to be able to print all records from list in table?
My jsp:
<c:forEach items="${list}">
<tr>
<td>${name}</td>
<td>${surname}</td>
</tr>
</c:forEach>
You're always printing the same request attributes, at each iteration of the loop, completely ignoring the current element of the list. Assuming the list contains objects of type Person (for example), which has a getName() and a getSurname() method, the code should be
<c:forEach items="${list}" var="person">
<tr>
<td>${person.name}</td>
<td>${person.surname}</td>
</tr>
</c:forEach>
Just like, in Java, a foreach loop would define a person variable for the current person during the itertion:
for (Person person: list) {
System.out.println(person.getName());
System.out.println(person.getSurname());
}
I have created a list of lists that contains content that I want to display through a jsp file. when trying to just display the items through one list, the file works and I see it. But when I split the items in different arraylists and try to iterate over that, nothing shows up. My initialization is,
private final List<ArrayList<DisplayableProduct>> listOfThreeProducts = new ArrayList<ArrayList<DisplayableProduct>>();
I have verified that there is content inside each list of it through debugging.
my model.listofThreePorducts is a list of lists. so I want to loop through the list of lists and then loop inside each loop and so stuff. Is it correct to pass the var="listoflists" value to the second for loop as such below? would it be items="${listoflists}" to access everything in that list ?
<c:forEach items="${model.listOfThreeProducts}" var="listoflists">
<div id="hero-featureSwap">
<c:forEach items="${listoflists}" var="product">
<div class="widget-element-brand"
title='<awsmp:formatText text="${product.vendorName}" />'>
<awsmp:formatText text="${product.vendorName}" maxLength="25" />
</div>
</c:forEach>
</div>
</c:forEach>
You need standard getter in model object for property listOfThreeProducts
further detail discussed in question's comment section
I have a list that tells me the getters to access for all objects in my form. As I iterate through the list, how can I convert that variable into the getter to call on the object? I'm trying to do something like the following but this is not correct as this is looking for getGetter on myObject.
<c:forEach var="myObject" items="${myForm.objects}">
<c:forEach var="getter" items="${myForm.getters}">
<html:text property="${myObject.getter}"/>
</c:forEach>
</c:forEach>
The reason I'm doing this is because I have a list of flex attributes for my object. I may only have a subset of the flex attributes defined. So the nested loop is iterating over the list of defined flex attributes. I'm not showing it here but in my code, I get the associated getter to call for the flex attribute.
I used a scriptlet inside my loop to do what I need. On my object I added a method called getGetterValue that takes in a string that identifies the getter to call. The method compares the string to lookup the getter to call, and then returns the value of the getter.
<c:forEach var="myObject" items="${myForm.objects}">
<c:forEach var="getter" items="${myForm.getters}">
<%
MyObject myObject = (MyObject)pageContext.getAttribute("myObject");
String getter = (String)pageContext.getAttribute("getter");
Object getterValue = myObject.getGetterValue(getter);
%>
<%= getterValue %>
</c:forEach>
</c:forEach>
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