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 Property(array object) in a Bean using EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Accessing Property(array object) in a Bean using EL" Watch "Accessing Property(array object) in a Bean using EL" New topic
Author

Accessing Property(array object) in a Bean using EL

Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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 ]

SCJP with 98% by...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

I won't repeat the trite phrase about assumptions.

If the data is as you say, the syntax is correct.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Originally posted by Erap Estrada:
I dont have JSP Spec here
This is something that you should remedy immediately.
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
I am missing something? The code for the bean is this:
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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

Joined: Nov 08, 2006
Posts: 92
A new problem occurs. I am trying to iterate throught it by:



But no results.

Thanks in advance!
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: Nov 08, 2006
Posts: 92
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Looks fine to me. Are you sure that you are using the correct version of the JSTL for your container?
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What gets printed if you add this to your jsp : ${cities}


[My Blog]
All roads lead to JavaRanch
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: May 16, 2005
Posts: 72
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

Joined: Nov 08, 2006
Posts: 92
Hi there.

Declaration in the top most of jsp page is:


Thanks again!
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

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

Joined: Nov 08, 2006
Posts: 92
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 "<cut>" is an unknown tag.

Thank you for your patience.
Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
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

Joined: Nov 08, 2006
Posts: 92
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
 
jQuery in Action, 2nd edition
 
subject: Accessing Property(array object) in a Bean using EL