how to create an array list of objects in JSP - java

I want to create an arraylist of objects in JSP.
And after that, want to loop through the list objects.
can some one please help me in creating it.

Create the ArrayList at servlet set it as attribute, and iterate it on JSP using <c:forEach>
Servlet
List<Foo> list = new ArrayList<Foo>();
list.add(foo1);
list.add(foo2);
list.add(foo3);
request.setAttaribute("fooList", list);
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
hello.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach items="${list}" var="foo">
<tr>
<td><c:out value="${foo.name}" /></td>
<td><c:out value="${foo.age}" /></td>
</tr>
</c:forEach>
Note: name and age are two properties of Foo with proper accessor methods

Related

Use ArrayList with servlet and jsp? [duplicate]

I am working on a project to try and teach myself spring and struts. I am currently stuck on a JSP page. I have a pojo class with variables eid and ename with getters/setters, I also have a table in sql with the same values with six populated rows.I am accessing my database through a JdbcTemplate and have stored the result in a list, I then passed this list to my action page in which I set it as a request.setAttribute("empList",eList). In my jsp page I call that attribute and then try to iterate through it using JSTL. However nothing shows up, I know that my list variable has data in it since i checked it using the expression tag <%=eList%> and objects show up like this:
[org.classes.database.Employee#d9b02,
org.classes.database.Employee#13bce7e,
org.classes.database.Employee#171cc79,
org.classes.database.Employee#272a02,
org.classes.database.Employee#137105d,
org.classes.database.Employee#1359ad]
I thought that maybe I was missing something on jstl but I have jstl-1.2 in my META-INF/lib folder. I have also tried to add it in the configure path file and still nothing. I also have the correct tag url. Also when I do a simple <c:out value="Hello"/>. Hello does print out. So this leads me to believe that my jstl is working properly, but when I try iterating through my list using jstl nothing shows up at all.Anyways here is my JSP page:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO- 8859-1"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# page import="java.util.List"%>
<!DOCTYPE html>
<% List eList = (List)session.getAttribute("empList");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employee Details</title>
</head>
<body>
<c:out value="Hello"></c:out>
<h3>Employee Details</h3>
<hr size="4" color="gray"/>
<table>
<%=eList%>
<c:forEach items="${eList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Any help would be highly appreciated!
Before teaching yourself Spring and Struts, you should probably dive a little deeper into the Java language. Output like this
org.classes.database.Employee#d9b02
is the result of the Object#toString() method which all objects inherit from the Object class, the superclass of all classes in Java.
The List sub classes implement this by iterating over all the elements and calling toString() on those. It seems, however, that you haven't implemented (overriden) the method in your Employee class.
Your JSTL here
<c:forEach items="${eList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
is fine except for the fact that you don't have a page, request, session, or application scoped attribute named eList.
You need to add it
<% List eList = (List)session.getAttribute("empList");
request.setAttribute("eList", eList);
%>
Or use the attribute empList in the forEach.
<c:forEach items="${empList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
change the code to the following
<%! List eList = (ArrayList)session.getAttribute("empList");%>
....
<table>
<%
for(int i=0; i<eList.length;i++){%>
<tr>
<td><%= ((Employee)eList[i]).getEid() %></td>
<td><%= ((Employee)eList[i]).getEname() %></td>
</tr>
<%}%>
</table>
you can read empList directly in forEach tag.Try this
<table>
<c:forEach items="${sessionScope.empList}" var="employee">
<tr>
<td>Employee ID: <c:out value="${employee.eid}"/></td>
<td>Employee Pass: <c:out value="${employee.ename}"/></td>
</tr>
</c:forEach>
</table>
<c:forEach items="${sessionScope.empL}" var="emp">
<tr>
<td>Employee ID: <c:out value="${emp.eid}"/></td>
<td>Employee Pass: <c:out value="${emp.ename}"/></td>
</tr>
</c:forEach>
another example with just scriplets, when iterating through an ArrayList that contains Maps.
<%
java.util.List<java.util.Map<String,String>> employees=(java.util.List<java.util.Map<String, String>>)request.getAttribute("employees");
for (java.util.Map employee: employees) {
%>
<tr>
<td><input value="<%=employee.get("fullName") %>"/></td>
</tr>
...
<%}%>

How to display multiple records in jsp using controller in spring?

enter image description hereI have a table of users data which have been retrived below using JdbcTemplate of Spring:
List<UserDetailsBean> userdetails = UserDetailsDaoObj.getallUserDataDetails(u.getId());
these details needs to be displyed in the jsp so im setting the object userdetails as below:
modelAndView.addObject("userdetails",userdetails);
I'm not able to retrieve the details in JSP the code used in JSP is below:
<c:forEach var="user" items="${userdetails}"><tr><td>${user.getId()}</td><td>${user.getAddress()}</td><td>${user.getCity()}</td><td>${user.getCountry()}/td></tr></c:forEach>
You just need to access properties of UserDetails like this
<c:out value="${user.id}" />
<c:out value="${user.address}" />
assuming that you have standard accessors method present in your class
You need to do something like this in JSP:
<c:forEach items="${userdetails}" var="user">
<tr>
<td>User ID: <c:out value="${user.id}"/></td>
<td>User address: <c:out value="${user.address}"/></td>
</tr>
</c:forEach>
You need to use JSTL tags to fetch values, In your case it is <c:out>.
Also try accessing elements using field name and not getters()
<c:forEach var="user" items="${userdetails}">
<tr>
<td><c:out value="${user.id}"/></td>
<td><c:out value="${user.country}"/></td>
</tr>
</c:forEach>
Well from what you posted its not really clear what your issue might be , but an obvious error , is what the others posted in their answers.
Note that * in order to operate properly , you have to place this line on top of your JSP.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Also the same for the jstl library. It must exist in your classpath.
link

JSP Page display date from collection

I am new to JSP and servlet.
I am trying to have list from servlet and wants to display those data into JSP page.
Here is what I did
My Servlet class
List<User> list = friendsDao.getFirendsList(user.getEmail());
System.out.println("List Size:"+list.size());
req.setAttribute("list", list);
getServletContext().getRequestDispatcher("/home.jsp").forward(req, resp);
My JSP Page
I have added this tag library
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
and here is what I am doing to iterate the data
<table>
<c:forEach var="friend" items="${list}">
<tr>
<td><c:out value="${friend}" /></td>
<td><c:out value="${friend.email}" /></td>
</tr>
</c:forEach>
</table>
but this is not working
but when I am trying to have something like this
<%
}
List<User> list = (List<User>) request.getAttribute("list");
%>
<table>
<c:forEach var="friend" items="<%=list%>">
<tr>
<td><c:out value="${friend.name}" /></td>
<td><c:out value="${friend.email}" /></td>
</tr>
</c:forEach>
</table>
This is also not working but, it at list iterate the loop to the size of data. but in browser it prints
${friend.name} ${friend.eamil}
How can I have actual values in there.
Please help me with this.
Thanks,
Nixit
change
<c:forEach var="friend" items="<%=list%>">
to
<c:forEach var="friend" items="${list}">
because by <%=list%> it is outputting the value right there, and you don't need reference to List<User> in jsp
Ohk I got the solution,
I don't know the reason, but jsp file requires me to put this one line of code. in order to tag library to work
<%# page isELIgnored="false" %>

if taglib comparing with two items of forEach jstl

I have an forEach taglib inside another one. I would like to compare one attribute of each object being listed in its respective taglib.
<c:forEach items="${andares}" var="andar">
..........
<c:forEach items="${lojas}" var="loja">
<c:if test="${loja.andar == andar.numero}">
.....
</c:if>
</c:forEach>
</c:forEach>
I would like to only execute some code when the attribute "numero" of object andar is equals to attribute "andar" of object loja.
How can I do it?
I don't recall, but it seems like that should work. If not, perhaps this?
<c:forEach items="${andares}" var="andar">
<c:set var="andarval" value="${anar.numero}"/>
..........
<c:forEach items="${lojas}" var="loja">
<c:set var="lojaval" value="${loja.andar}"/>
<c:if test="${lojaval == andarval}">
....
</c:if>
</c:forEach>
</c:forEach>

Add values to arraylist use JSTL

is it possible to add values to an ArrayList instead of using a HashMap
something like:
<jsp:useBean id="animalList" class="java.util.ArrayList" />
<c:set target="${animalList}" value="Sylvester"/>
<c:set target="${animalList}" value="Goofy"/>
<c:set target="${animalList}" value="Mickey"/>
<c:forEach items="${animalList}" var="animal">
${animal}<br>
</c:forEach>
now getting the error:
javax.servlet.jsp.JspTagException: Invalid property in <set>: "null"
thx
JSTL is not designed to do this kind of stuff. This really belongs in the business logic which is (in)directly to be controlled by a servlet class.
Create a servlet which does like:
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
List<String> animals = new ArrayList<String>();
animals.add("Sylvester");
animals.add("Goofy");
animals.add("Mickey");
request.setAttribute("animals", animals);
request.getRequestDispatcher("/WEB-INF/animals.jsp").forward(request, response);
}
Map this on an url-pattern of /animals.
Now create a JSP file in /WEB-INF/animals.jsp (place it in WEB-INF to prevent direct access):
<c:forEach items="${animals}" var="animal">
${animal}<br>
</c:forEach>
No need for jsp:useBean as servlet has already set it.
Now call the servlet+JSP by http://example.com/context/animals.
To do add() to a List or others methods from Map, Set, etc... You have to use a unusable variable.
<jsp:useBean id="list" class="java.util.ArrayList"/>
<c:set var="noUse" value="${list.add('YourThing')}"/>
<c:out value="${list}"/>
The above code is not working.
Following are the lines of code that has to be placed in file animals.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:forEach var="animal" items="${animals}">
<c:set var="animalName" value="${animal}"/>
<c:out value="${animalName}"/>
</c:forEach>

Categories