This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes jsp:useBean && forEach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "jsp:useBean && forEach" Watch "jsp:useBean && forEach" New topic
Author

jsp:useBean && forEach

Luke
Greenhorn

Joined: Sep 14, 2005
Posts: 10
Hi

The below code works fine using JSTL

<H3> This is an example for JSTL </H3>
<c:forEach var = "myBean" items= "${requestScope.LukeObject}" varStatus ="MyBeanCounter">
Bean value of ${MyBeanCounter.count} is ${myBean.intA}
</c:forEach>


This is an example for JSTL
Bean value of 1 is 0 Bean value of 2 is 1 Bean value of 3 is 2

But the below code is not working (useBean && JSTL combination)

<H3> This is an example for useBean </H3>
<jsp:useBean id ="myJSPBean" class ="Foo.Bean" scope ="request"/>
<c:forEach var ="myJSPBearVar" begin = "1" step = "1" items = "${myJSPBean}">
Bean value of ${begin} is ${myJSPBearVar.intA}
</c:forEach>

I get the below error

javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

Can any one help me out how to use useBean and forEach together?

Thanks
Luke
[ October 11, 2005: Message edited by: Luke ]
Mat Williams
Ranch Hand

Joined: Jul 20, 2005
Posts: 215
Hi Luke,

for this line to work
<jsp:useBean id ="myJSPBean" class ="Foo.Bean" scope ="request"/>

it requires an object stored in the request under the name "myJSPBean". That is the servlet will need to have done the following


then in the JSP, the useBean tag will get that bean and make it available to other tags. If the servlet has not created it the useBean tag will create it for you.

when you get to this bit
<c:forEach var ="myJSPBearVar" begin = "1" step = "1" items = "${myJSPBean}">

the EL ${myJSPBean} will go looking for an object in some scope with that name which makes the useBean completely redundant.

The jsp tags where thought of and created before EL and so you needed a way to get and set properties on beans etc, this is what the <jsp:...> tags do. These days EL mostly replaces the need to use the <jsp:...> tags.

HTH

Mat
Troy Peter
Greenhorn

Joined: Oct 13, 2005
Posts: 14
Hi Luke, hi Mat,

Mat just gave a very good answer to your question. I'd like to add that the class Foo.Bean should be a collection for the tag c:forEach to work.

Regards,
Troy.
 
 
subject: jsp:useBean && forEach
 
Similar Threads
jdbc connection in jsp
Where does JSTL $data come from?
jsp:useBean - doubt in question
JSTL forEach
How to display arraylist value in jsp, using JSTL tags?