Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing an ArrayList of objects in JSP

 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an ArrayList of Product items created in a servlet and passed into a jsp that I need to display in the jsp. Can someone point me to a howto on this or some examples that show how to iterate through the list and display the fields of the Product objects stored in the ArrayList? I'm not particularly familiar with Beans yet so I will be reading up on that. Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out JSTL's <c:forEach> tag.

A link from the Ranch's FAQ:
http://www.onjava.com/lpt/a/1500
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I installed Taglibs and set up 'core' to be part of my web app. Now I made a little test jsp. I have a class called "Customer" in my WEB-INF/classes folder (and sources of course), "results" is an ArrayList of Customer objects that is stored in the session:

But I'm getting the "cannot be resolved to a type error" on my "Customer" class which is sitting in the WEB-INF/classes folder.

[ April 30, 2006: Message edited by: Lido Collins ]
 
Eduardo Dela Rosa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

To easily isolate the problem, you may want to create a package
for the Customer class (i.e., do not use default package) and
you may want to include it in the list of imports in your JSP
page directive.

hth.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of the way that classLoaders work in web applications, it's never a good idea to use the default package. Always put your classes in packages when creating a web application.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I will look up how to make the package.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added this line to my class:

and put the compiled class in: WEB-INF/classes/storePackage/

Then I changed my jsp to:

Now I'm getting this:


[ April 30, 2006: Message edited by: Lido Collins ]

[ April 30, 2006: Message edited by: Lido Collins ]
[ April 30, 2006: Message edited by: Lido Collins ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need any <jsp:usebean> or <%@ page import%> tags for this. At some point before displaying this JSP you need to put the ArrayList in the session like this:


Then in your JSP, all you need is:

 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't I need a bean to handle the fact that Customer is an object?

Now I think I have a problem with my jstl installation. I redeployed my war file and had to copy the two jars into the lib folder and the .tld files into the WEB-INF file and I'm not sure it's working. Here's my new test file based on your suggestion:

Here are the results:


[ April 30, 2006: Message edited by: Lido Collins ]
[ April 30, 2006: Message edited by: Lido Collins ]
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the expressions are coming out like that then one of two things is wrong

1 - You are using the wrong version of JSTL (JSP1.2 eg Tomcat4)
2 - you haven't updated the web.xml file on your server. (JSP2.0 - eg Tomcat5)

Please see the FAQs on this subject:

http://faq.javaranch.com/view?ElOrJstlNotWorkingAsExpected
http://faq.javaranch.com/view?ServletsWebXml
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Tomcat 5, and I think it's JSTL 1.1.2 which when I downloaded said it would work with jsp/servlets under Tomcat 5 because all containers are compatible (something like that).
Also, the standard-examples are working... I even tried copying the standard-examples web.xml <web-app> tag. Commenting this out:

and adding this:


[ April 30, 2006: Message edited by: Lido Collins ]
[ April 30, 2006: Message edited by: Lido Collins ]
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, if you have Tomcat 5, then that last change should almost be enough.
The ony remaining change would be to put <jsp-config> around those taglib definitions in web.xml. At that point all should start working.

Also note that taglib entries for JSTL are NOT necessary, and in fact can create problems. All you need to use the JSTL tags are the two jar files in WEB-INF/lib (standard.jar, jstl.jar)
- No tld files
- No entries in web.xml
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but that didn't help. Still getting the same output. I tried taking the .tld files out of the directory and taking the <taglib> section out of web.xml first, but that didn't work. Then I put the tld files back, added the <taglib> section back into the web.xml file and then put the <jsp-config> tags around that, still no change.
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do have

<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 web-app_2_4.xsd" version="2.4">

in web.xml, rather than that DTD thing?

The other trick is to try this at the top of your jsp page:
<%@ page isELIgnored="false" %>

If you have web.xml set up correctly, you shouldn't need this, but it might be worth a try.
 
Eduardo Dela Rosa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't actually see problem with your taglib directives, nor taglibs in web
descriptor file, but if you're getting the same problem, i.e., invalid Customer object, try looking back at your 17th post.

Is it right that you moved your Customer class to "storePackage"?... then why did you declare a "com.storePackage.Customer" class in your <jsp:useBean>
action tag when it should only be "storePackage.Customer" (whithout the com)?

But please ignore this if you already have restructured your codes then. It's
just a thought of the past.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have that web-app tag instead of the DTD etc tag in web.xml. I will try that line in the jsp and see what happens. I will also double check the package situation. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic