• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

iterating ArrayList in forEach

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
$(testattribute} -> ${testattribute}
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

see your
---------------------------------
items="$(testattribute}"
-----------------------------

you are putting $(} insteadof ${}

thanks & regards,
seeharaman.v
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Smitha DN",
Please check your private messages.
-Ben
 
Smitha Jain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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" %>
 
Smitha Jain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"?
 
Ranch Hand
Posts: 598
3
jQuery Google App Engine Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think so because I do the same.

best regards,
omi
 
Smitha Jain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic