aspose file tools*
The moose likes JSP and the fly likes jsp Iterate tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp Iterate tag" Watch "jsp Iterate tag" New topic
Author

jsp Iterate tag

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all

Is there some kind of jsp itereate tag similar to the struts iterate tag that would allow me to loop through a list of objects and output the data in table rows? My new project does not use Struts and i don't want to have to use scriplets in my jsp page.

Thanks,

AMD
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Within JSTL (Java Standard Tag Library) there is the forEach tag.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

A link to the JSTL specification is available in the JSP FAQ.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Thanks guys

So do you think that would work well with this situation.

I have an array of user objects that contain stuff like first name, last name ...

I am setting that array on a userBean. I am then going to use jsp:useBean and jsp:getProperty to get a hold of my array of user objects. I then want to use the JSTL iterate tag to go through the array and print out all the users to a table row by row.

Sound good?

Thanks,

AMD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Yuppers, except that instead of getProperty I'd use the more moders EL/JSTL approach to retrieve the property values.

By the way, what version of JSP are you on?
[ August 24, 2006: Message edited by: Bear Bibeault ]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Bear,

I think 1.2. Do I have to copy those jars to my project lib or can I use them since they are installed on my web server. I am using Jboss.

Thanks,

AMD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

They're probably not pre-installed with the Tomcat instance that JBoss is using.

If you can, I'd highly recommend upgrading to a JSP 2.0 version.

Be sure you obtain the right version of JSTL for your version of JSP. It's all covered in the JSP FAQ.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Ok I looked closer. I am using the Tomcat 5.5, which I think has the the 2.0 container. I got the 1.1 JSLT jars and installed them in my projects lib. Now do I have to add something to the web.xml? Right now it is not working, I poked around online and it seemed some people were added stuff to web.xml. Any thoughts?

Thanks,

AMD
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

What do you mean by "not working"? You don't need to add anything in the web.xml but you do need to add the taglib declarations in your JSP file(s). This is also in the JSP Faq.


GenRocket - Experts at Building Test Data
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Sorry "not working" means I tried this test

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

Setting the value: "Hello World!"
<c:set var="hello" value="Hello World!"/>
<p/>
<cut value="${hello}"/>

The output is $(hello) I get a warning in Eclipse saying unknow tag cut. I think I have the correct jars because previoulsy I was just getting a blank page.

Thanks,

AMD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Andrew Mcmurray:
I poked around online and it seemed some people were added stuff to web.xml.


Gregg is correct; no web.xml dickering necessary.

Your online research reveals that most people don't know that and, quite incorrectly, think that they need to extract the TLD files from the jar files, put them in WEB-INF, and then place elements in the web.xml to reference them. This is not necessary and will just lead to problems later on down the road. Don't do it.
[ August 24, 2006: Message edited by: Bear Bibeault ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

If you are using JSTL 1.1, the correct syntax is:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Which can be copy and pasted from...you guessed it, the JSP Faq. ;)
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Ok thanks Greg, I think I am getting close. I didn't think the order mattered with the prefix and uri. So now Eclipse recognizes the tags, but the output is still the same as above just ${hello} instead of the var value. Any thoughts?

Thanks,

AMD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Is your web.xml configured as a servlets 2.3. or 2.4 web application? (Covered in JSP FAQ, as per usual).
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Hmm, not too sure. It works fine for me. You have the jstl.jar and standard.jar in the WEB-INF/lib folder?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Originally posted by Bear Bibeault:
Is your web.xml configured as a servlets 2.3. or 2.4 web application? (Covered in JSP FAQ, as per usual).


Ahhh. I bet that is more likely it.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Bear,

My web.xml says Web Application 2.3.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

<imagine a game show buzzer going off>

If so, you have effectively disabled all JSP 2.0 features in your application.

Fix it as described in the JSP FAQ.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Bear,

I looked through the faq, I am not sure if I am looking in the right spot, but from what I found I got the wrong version of the JSTL jars? I should of got the 1.0 instead of the 1.1 ones?

Thanks,

AMD
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

No, you need the 1.1 jars but you need to change your web.xml to be for 2.4.

See here and copy and paste. ;)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

NO

Why are you making things os hard on yourself. Just set up your web.xml properly an be done with it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

BONK
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

I've added a new entry to the JSP FAQ that gathers the steps to set up the JSTL 1.1 for JSP 2.0 in one place.

I also removed some older JSTL entries that just seemed to be confusing people.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Thanks Gregg and Bear After I switched the web.xml it worked!

AMD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp Iterate tag