• 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

Accessing Property(array object) in a Bean using EL

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive searched the forum and google but can't find the solution from what i really need.

I have a Bean that has a property that's an array object.

How can I retrieve the data in the array from a bean using EL alone(no script)?

I invoke the bean in my jsp page using:



Thanks!
[ May 25, 2007: Message edited by: Erap Estrada ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. You can iterate over the array property values using <c:forEach> or you can access indexed values directly with the [ ] operator.

Do you not have the JSP Spec handy and opened to the section on the EL?
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont have JSP Spec here.

I tried this


<jsp:useBean id="cities" class="com.company.util.LocationBean" scope="request" />

${cities.locations[0]}



Where "locations" is a bean property that's an array. But the page does not load completely. So I assume, there's a problem with that syntax.

Thanks for quick response.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I won't repeat the trite phrase about assumptions.

If the data is as you say, the syntax is correct.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Erap Estrada:
I dont have JSP Spec here

This is something that you should remedy immediately.
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am missing something? The code for the bean is this:
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. The bean is not initializing it. I thought it is calling the constructor.

Thank you so much for the help!
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A new problem occurs. I am trying to iterate throught it by:



But no results.

Thanks in advance!
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help.

The samples on books and online are arrays of bean objects. But I would like to iterate through an array of String objects.

Thanks in advance!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No difference whether its a bean or a string. Your syntax is fine. Check the state of the bean and array. If you're not getting any output, it's probably because the array is empty.
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I confirmed that the array is not empty by printing out one of its values. I am trying to iterate through the array using the following:


Where "cities" is the bean id and "locations" is an array property inside the bean.

What's wrong with the code above?

Thanks in advance!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks fine to me. Are you sure that you are using the correct version of the JSTL for your container?
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How will i confirm that?

How would i know which version to use?

I am using apache tomcat 5.5 and in my web.xml there is "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd", which i assume as JSTL declaration.
[ May 29, 2007: Message edited by: Erap Estrada ]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Erap Estrada:
How will i confirm that?
How would i know which version to use?

It's all in the JSP FAQ.

I am using apache tomcat 5.5 and in my web.xml there is "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd", which i assume as JSTL declaration.

Umm, no. That has nothing to do with the JSTL; it delcares your web app as a using servlets 2.4.

What URI are you using for the JSTL tag declarations?
 
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
What gets printed if you add this to your jsp : ${cities}
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have a configuration problem.
Make sure you have used the following:

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

2)In web.xml:
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>;
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
</jsp-config>

3) web.xml and c.tld files should be in WEB-INF directory.

4) jstl.jar and standard.jar should be in WEB-INF\lib directory.

You can use the following URL to install .tld and other jar files.
http://java.sun.com/products/jsp/jstl
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Suman Sharma:
Make sure you have used the following:

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



Not correct!

That URI is for the JSTL 1.0 which is not the correct version for Tomcat 5.5. Rather, the JSTL 1.1 should be used. It has a differrent URI -- please see the JSP FAQ for details.

2)In web.xml:



Also not correct. Do not put any declaration for the JSTL in web.xml. It is unecessary and creates just another point of potential misconfiguration.

3) web.xml and c.tld files should be in WEB-INF directory.



Incorrect again. web.xml should be in WEB-INF, but c.tld should never be extracted from the jar file and placed anywhere.
[ May 30, 2007: Message edited by: Bear Bibeault ]
 
Suman Sharma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried Erap's example using the configuration that I mentioned before. It is working completely fine. There might be a backward compatibility.

But thanks for the infomation about how to use the new version of JSTL. It will be helpful.
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there.

Declaration in the top most of jsp page is:


Thanks again!
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. I am really confused now with the version type and cant find a documentation that will point me with the compatibility of technologies i used.

I basically have

Java 1.4
Struts 1.3.8

Now, i have no idea what JSP, Servlets, JSTL, I am using. I just downloaded the libraries and import it.

THanks!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Declaration in the top most of jsp page is:



These are incorrect. You are using URIs for JSTL 1.0 and should be using JSTL 1,1.

Now, i have no idea what JSP, Servlets, JSTL, I am using.



This is important information for you to have before you write a single line of code.

With Tomcat 5.5, if your web.xml is correctly configured, you'll be using Servlets 2.4 and JSP 2.0.

With the above you must use JSTL 1.1.

I keep referring you to the JSP FAQ which contains all this information. Why are you ignoring it?
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I am using Tomcat 5.5 that uses JSP 2.0. So I am using JSTL 1.1.

Now, as i correct the URI from


to this


Eclipse now is complaining that "<c:forEach>" and "<c:out>" is an unknown tag.

Thank you for your patience.
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear. If you're referring to this page

http://java.sun.com/products/jsp/faq.html

I don't see any answer to my previous questions there.

Thanks!
 
Erap Estrada
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear. I did not know you're referring to the JSP FAQ on your signature. I keep referring to the Sun website.

It is working now.

Thank you so much for being with me on these 3 days ordeal
 
machines help you to do more, but experience less. Experience this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic