• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Acess list elements on foreach JSTL tag

 
Thiago Leobons
Greenhorn
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Thiago Leobons
Greenhorn
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1640
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the error?
 
Thiago Leobons
Greenhorn
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's it? Usually there's more info. Have you looked in the logs?
 
Thiago Leobons
Greenhorn
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic