JSP table from ArrayList creation - java

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());
}

Related

why does this work in jstl with hashmaps?

why does display what i want:
<c:forEach var="temp" items="${AvailableLessonBean.lessons['description']}">
<form action="" method="POST">
<tr>
<td>
<c:out value="${temp}"/>
</td>
which displays in a html table:
Description Start Date Start Time End Time Level Make Booking
Snowboarding for dummies
Advanced Carving Techniques
How to not fall off the draglift
Gnarliness Extreeeeeeeme
Parallel turns
How to splint a broken leg with a s
Cross-country techniques
Aerobatics
Intermediate Slalom
and this does not display anything but an exception:
<c:forEach var="temp" items="${AvailableLessonBean.lessons}">
<form action="" method="POST">
<tr>
<td>
<c:out value="${temp['description']}"/>
</td>
I have no hair left because of this!!
Once again, you didn't tell us what these objects are, which makes it impossible to answer. Hopefully for you, I remember the data structure from a previous question.
So, AvailableLessonBean.getLessons() returns a Map<String, List<String>>.
In your first snippet, you iterate over AvailableLessonBean.lessons['description']. What this EL code does is that it gets the value associated with the key "description" in the map:
map.get("description")
that works fine. It returns a List<String>. It iterates over that list, and stores the current element of the list in a variable named temp. And at each iteration, it prints temp. This is thus equivalent to the following Java code:
List<String> list = availableLessonBean.getLessons().get("description");
for (String temp : list) {
out.println(temp);
}
In the second snippet, you're iterating over the map itself. The JSTL specifies that when you iterate over a map, you iterate over all the entries of the map. Each entry has a key and a value. So the iteration is equivalent to doing, in Java:
for (Map.Entry<String, List<String>> temp : availableLessonBean.getLessons().entrySet()) {
...
}
But then you're trying to access temp["description"]. That doesn't make any sense. temp is a Map Entry. A Map Entry has a key and a value. That's all. In this case, the key is a String and the value is a List<String>.

Access list of list in JSP file

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

Having exception in accessing a list through JSTL

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.

Struts 1 how to access an object's getter based on a variable

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>

JSTL - Assign list content into an array

I have list of users and I want to assign each user into an index of array:
<%
User users[] = new User[n];
pageContext.setAttribute("users", users);
%>
Now:
<c:forEach items="${usersList}" var="user">
// here I want to assign each user into an array index like:
// users[index] = user
</c:forEach>
At the end I want to access each index of array manually. Something like below:
<p><c:out value="${users[0].getName()}" /></p>
I know above is not correct, can some one help how can I achieve this via jstl? Thanks.
P.S: Please be noticed that I want to access the array by index manually.
You can use varStatus to get index in the loop
<c:forEach items="${usersList}" var="user" varStatus="status">
Index: ${status.index}
</c:forEach>

Categories