Access list of list in JSP file - java

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

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>.

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.

Struts2 <s:select list=""> not working dynamically

I am using Struts2.
<s:iterator value="empReportFields" var="empReportField"
<s:select name="%{#empReportField.fieldName}" list="%{#empReportField.listName}" listKey="id" listValue="name" cssClass="search" headerValue="All" headerKey="All" />
<s:property value="#empReportField.listName" />
// Here it is displaying proper list name
</s:iterator>
I am fetching out these data from my db. Now I am displaying specific list in select box (<s:select list="<ListName>" />) which is stored in column of my table (Database).
Normally it runs like.
<s:select name="emp" list="locationList"
listKey="id" listValue="name"
headerValue="All" headerKey="All" />
It will work well.
But I find simple select box with no list value in it. So what is the actual problem??
In short I want to call list dynamically.
The s:select tag itslef has a list attribute where you can directly give the name of the list (in action class) you want to fill the dropdown with. You do not need an iterator for packing values into s:select dropdown.
Try this:
<s:select label="Select from here"
headerKey="-1" headerValue="Select"
list="listNameHere"
name="feildNameHere" />
Here 'listNameHere' is the list in action layer and the 'feildNameHere' is a instance variable in action class which recieves the value selected by the user.
According to how I understand the question you want to load <s:select list dynamically according to the iterator value.
If so just use the <s:select list like this <s:select list="#empReportField.listName".
Where <s:iterator value="empReportFields" is a list and inside that list another list or map named listName.

Access a list within a dropdown in Javascript

Im trying to access a list within a dropdown in Javascript. So I have an object called System (which is the object stored in the dropdown) and this has a List of Collections. I am new to Javascript and not sure what the correct syntax should be to access the list.
So this is the form that contains the dropdown:
<form:form id="systemForm" method="post" action="/application/SystemSave" commandName="systemForm">
<form:select path="uSelectedSystemId" id="uSystemId">
<c:forEach var="system" items="${systemsList}">
<form:option value="${system.id}" label="${system.name}" />
</c:forEach>
</form:select>
</form:form>
and this is the setup of the System object (from Java):
public class System {
private Long id;
private String name;
private List<Collection> collections;
}
and this is the setup of the SystemForm used to store the values:
public class SystemForm {
private String fUpdateSystemName;
private String uSelectedSystemId;
private List<Collection> uCollections;
}
I guess I need some sort of hidden field within the loop that stores the values but im not sure of the correct syntax. I could just rehit the database and get all Collections based on the systemID but I dont think this is the correct way of doing things seeing as the information should already be available.
Any help much appreciated!
try this
<form:form id="systemForm" method="post" action="/application/SystemSave" commandName="systemForm">
<select id="uSystemId">
<c:forEach var="system" items="${systemsList}">
<option value="${system.id}" label="${system.name}" />
</c:forEach>
</select>
</form:form>
i dont know exactly this will meet your requirement or not.

How to access a java list coming from the server in javascript inside my jsp

Plz consider the scenario. I have a java class called Person as below:
Person
---------
Integer Id
String name
String address
Now through my spring controller I pass a list of persons to my jsp page(neighbors.jsp) like shown below:
List<Persons> persons = new ArrayList<Person>();
.
.
.
return new ModelAndView("/neighbors").addObject("persons", persons);
Now the problem is here. I have the google maps api in javascript format embedded in neighbors.jsp to display the location of the person logged in. This works fine.
Google maps also offer comparison of addresses. I want to display markers of addresses of other persons that are within 5 miles range of user's address. Each of the marker is a link to a page that is going to display that particular person's information.
Suppose I access each address in the following format, how do I call the javascript function?
<c:forEach items="${persons }" var="person">
<!-- I want to pass each address ${person.address} to the javascript functions thats going to compare addresses -->
</c:forEach>
Can someone help me out here on how to handle to scenario?
Two ways to do it:-
First way... you can set the value as a hidden field that allows the javascript to access it:-
<c:forEach items="${persons}" var="person" varStatus="i">
<input id="address${i.count}" type="hidden" value="${person.address}">
</c:forEach>
In your javascript:-
yourJavascriptFunction(document.getElementById("address1").value);
Second way... use <script> tag in your <c:foreach> tag:-
<c:forEach items="${persons}" var="person" varStatus="i">
<script>
yourJavascriptFunction("${fn:replace(person.address, "\"", "\\\"")}");
...
</script>
</c:forEach>

Categories