| Author |
iterating ArrayList in forEach
|
Smitha Jain
Greenhorn
Joined: Apr 23, 2008
Posts: 11
|
|
I am new to JSTL, trying out some examples on EL. Have set an ArrayList to request/session in Servlet. Am trying to iterate the List in JSP using c:forEach. Have used: JSTL1.1, JSP2.0, Servlets2.4, Tomcat5.x Servlet: req.setAttribute("testattribute", userList); sessionObj.setAttribute("userlist", userList); //userList is the ArrayList JSP: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> ${userlist} <br /> ${testattribute} <br/> <c:forEach var="product" items="$(testattribute}"> <tr><td>${product}</td></tr> </c:forEach> </body> </html> Output: [admin, superuser, user, enduser] [admin, superuser, user, enduser] $(testattribute} Its not looping with the request attribute value. Can you let me know, whats wrong in the above code?
|
Thanks & Regards,<br />Smitha
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
|
$(testattribute} -> ${testattribute}
|
[My Blog]
All roads lead to JavaRanch
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Hi, see your --------------------------------- items="$(testattribute}" ----------------------------- you are putting $(} insteadof ${} thanks & regards, seeharaman.v
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
"Smitha DN", Please check your private messages. -Ben
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Smitha Jain
Greenhorn
Joined: Apr 23, 2008
Posts: 11
|
|
Thanks ya.... I overlooked it. I tried changing as below: <c:forEach var="product" items="${testattribute}"> <tr><td>${product}</td></tr> </c:forEach> now I am getting error: org.apache.jasper.JasperException: According to TLD or attribute directive in tag file, attribute items does not accept any expressions Am getting error @ <c:forEach> line.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Hi , that means your "testattribute" is not an array/list object... please post your testattribute values which are in servlet thanks & regards, seetharaman.v
|
 |
Smitha Jain
Greenhorn
Joined: Apr 23, 2008
Posts: 11
|
|
i am trying an example for EL. Here is the code snippet frm Servlet: ArrayList userList = null; userList = new ArrayList(); userList.add("admin"); userList.add("superuser"); userList.add("user"); userList.add("enduser"); sessionObj.setAttribute("userlist", userList); req.setAttribute("testattribute", userList); ${testattribute} in JSP, is displaying all the values in the ArrayList. But the c:forEach is not working. I tried doing it with HashMap as well that also didnt work. I have downloaded the jstl.jar and standard.jar for JSTL1.1. And extracted the .tld files from standard.jar. Do I have to add any other jar or tld file?
|
 |
sammeta Phanikumar
Ranch Hand
Joined: Oct 25, 2007
Posts: 81
|
|
please try this..by chanig your jsp. with these changes <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false" %>
|
SCJP 5, SCWCD 5, SCDJWS 5
|
 |
Smitha Jain
Greenhorn
Joined: Apr 23, 2008
Posts: 11
|
|
Hey it worked..... Thanks I have added the ELEnable code in web.xml. <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-enabled>true</el-enabled> <scripting-enabled>true</scripting-enabled> </jsp-property-group> </jsp-config> I dont know why thats not working. I have one doubt: For every page do I have to add isELIgnored="false"?
|
 |
omi sharma
Ranch Hand
Joined: Mar 18, 2008
Posts: 489
|
|
I think so because I do the same. best regards, omi
|
SCJP, OCA 9i application developer, SCWCD 5.
When I was in hell someone told me to get heaven you need to do Java.
|
 |
Smitha Jain
Greenhorn
Joined: Apr 23, 2008
Posts: 11
|
|
I read some where that in JSP2.0 by default: <%@ page isELIgnored="false" %> <!-- default for JSP 2.0 --> If we are using JSP1.2, then we have to set the value as false for EL externally. I am using JSP2.0. But still code not working without isELIgnored=false. And also we have two options to set the EL flag either in web.xml or in page directive in JSP. Tried adding it in web.xml, its not working. <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <el-enabled>true</el-enabled> <scripting-enabled>true</scripting-enabled> </jsp-property-group> </jsp-config> </web-app>
|
 |
 |
|
|
subject: iterating ArrayList in forEach
|
|
|