File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Accessing an ArrayList of objects in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Accessing an ArrayList of objects in JSP" Watch "Accessing an ArrayList of objects in JSP" New topic
Author

Accessing an ArrayList of objects in JSP

Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Check out JSTL's <c:forEach> tag.

A link from the Ranch's FAQ:
http://www.onjava.com/lpt/a/1500


[My Blog]
All roads lead to JavaRanch
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
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

Joined: Apr 15, 2004
Posts: 25
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.


Eduardo dela Rosa<br />SCJP 1.4
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
Thanks, I will look up how to make the package.
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Apr 27, 2006
Posts: 20
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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Apr 27, 2006
Posts: 20
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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Apr 27, 2006
Posts: 20
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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Apr 15, 2004
Posts: 25
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

Joined: Apr 27, 2006
Posts: 20
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing an ArrayList of objects in JSP