This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JSP and the fly likes Acess list elements on foreach JSTL tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Acess list elements on foreach JSTL tag" Watch "Acess list elements on foreach JSTL tag" New topic
Author

Acess list elements on foreach JSTL tag

Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Hi, this is my first question in here (you can even say I registered just to ask it, true story.. I hope I can participate more in here from now on nonetheless)

So this is what I wanna do...

show a JSP with a table containing smthg like: (please pretend the pipes form a straight table since I don't know how to work around the forum's autoformat):

John | john@gmail.com | adress 1
(nothing) | (nothing) | adress 2
Mary | Mary@hotmail.com | adress 1
(nothing) | (nothing) | adress 2

and the table goes on..

I did it like this with a scriplet:


but how can I access the fields of the list Addresses that's inside User using EL or whatever tool JSTL tags allow me to use?

Thanks for your help!


OCJP - CHECK
OCWCD - IN PROGRESS
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

Firstly, you'd create the list in the page controller for the JSP, not in the JSP itself. Then you'd set it as a scoped variable in the request (with setAttribute()) and forward to the JSP from the controller.

Then, use the JSTL <c:forEach> tag to loop through the list.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

I know... that's how I was trying to do it with the <c:forEach> tag (not calling the function from the JSP, which I only did to show what I want to be shown in the table) but I'm having a hard time accessing the field elements from a List but only using scoped variables won't solve the problem.
It would be very helpful if you give me a few lines on how it's done
Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

Well, looking at your example, the forEach tag might look something like: (though I'd use something more descriptive than u for a variable name).

Then, within the loop, use the EL to access the properties; such as: ${u.name} and ${u.email}. To iterate over the inner list of addresses, you'd use another forEach loop on the property ${u.addresses}.





Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Ok, here is my attempt at it.
Obviously the java scriptlet code should be shifted to a servlet that forwards to the jsp.
Your code didn't actually user the AddressDao - is it meant to?

The only slight twist to this example is how to print out the username for the first address, but not on any others.
I used a <c:choose> statement, and took advantage of the loopStatus variable on the <c:forEach> tag so that it will print out user details for the first address, but not any of the following ones.



cheers,
Stefan
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Thanks for your answer Stefan

You're right, the UserDao instance is left from another code from previous attempts, please disconsider it.
About your code, that was really the way I was trying to iterate over the address list on user but I keep getting an error

org.apache.jasper.JasperException: An exception occurred processing JSP page /list.jsp at line 39

39: <c:forEach var="address" items="${user.addresses}" varStatus="addressLoop">

so idk.. have you tried this code yourself and it worked fine??

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

What's the error?
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Thiago Leobons wrote:
org.apache.jasper.JasperException: An exception occurred processing JSP page /list.jsp at line 39

39: <c:forEach var="address" items="${user.addresses}" varStatus="addressLoop">



That's the error I'm getting
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

That's it? Usually there's more info. Have you looked in the logs?
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

oh sorry I'm gonna copy the whole stack trace.

There it goes:

org.apache.jasper.JasperException: An exception occurred processing JSP page /list.jsp at line 39

39: <c:forEach var="address" items="${user.addresses}" varStatus="addressLoop">

and then...

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
control.Control.list(Control.java:38)
control.Control.execute(Control.java:26)
control.Control.doGet(Control.java:17)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entity.User.Addresses, no session or session was closed
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:249)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:348)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:224)
org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
org.apache.jsp.lista_jsp._jspx_meth_c_005fforEach_005f1(lista_jsp.java:182)
org.apache.jsp.lista_jsp._jspx_meth_c_005fforEach_005f0(lista_jsp.java:144)
org.apache.jsp.lista_jsp._jspService(lista_jsp.java:105)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
control.Control.list(Control.java:38)
control.Control.execute(Control.java:26)
control.Control.doGet(Control.java:17)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:722)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entity.User.Addresses, no session or session was closed

There it is. You have a Hibernate transaction boundary issue. Looks like you are closing the Hibernate session before forwarding to the JSP?
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Yes, now it works!! Thanks both of you Bear and Stefan!
Now where should I close the Hibernate connection then? Also, the values on ${user} were showing just fine, why the ones on ${address} were not?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

You must have the entity set up such that addresses list is lazy loaded -- that is, not fetched until it's asked for. You can either change the setting so that the list is pre-fetched when the main entity is fetched, or look into "open session in view".
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

But I didn't configure this in any entity, why would one of them be "lazy" and the other one not?
And where do I close my Hibernate connection now that I have to leave it open until the JSP is shown?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

Thiago Leobons wrote:But I didn't configure this in any entity, why would one of them be "lazy" and the other one not?

It will use defaults if you haven't specified. I believe that the default for Lists is to lazy-load them.
And where do I close my Hibernate connection now that I have to leave it open until the JSP is shown?

It'll likely be easier to just make sure the list is loaded before heading off to the JSP if this is the only issue you'll be having with lazy loading. Otherwise, l already told you that the concept to search for is "open session in view".
Thiago Leobons
Greenhorn

Joined: Sep 04, 2011
Posts: 20

Ok thanks a lot again ! I'll look more into the Hibernate loading options now

I think we can declare it SOLVED!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60080
    
  65

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Acess list elements on foreach JSTL tag
 
Similar Threads
Servlet or JSP Issue, I'm not sure, it's my first time with both.
Number Format Exception
page refresh problem
Displaying the right details of a resource taken from a JSP results page
Take the quiz: Which sci-fi crew would you fit best in?