Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp:useBean && forEach

 
Luke
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic