| Author |
how to use c:forEach....
|
Vijay Vaddem
Ranch Hand
Joined: Feb 13, 2004
Posts: 243
|
|
Hi, I have a problem.....(hope i will get an answer..) I have the following piece of scriplet in my jsp and i want to display it using expression language.... <% masterData mData = session.getAttribute("databean"); for(int i = 0; i < mData.size(); i ++) { childBean cBean = mData.getChildBean(); cBean.getText(); cBean.getAnnotation(); cBean.getUrl(); } %> Hope im clear in my message ..........
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
What have you tried? Why not post your first attempt at it an we will take it from there. Also, is mData a collection class?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Vijay Vaddem
Ranch Hand
Joined: Feb 13, 2004
Posts: 243
|
|
Hi, mBean is a custom bean which is storing childBean..... and child bean has the above 3 methods.... So, at one stage, I may have 1 mBean with 3 childBeans nested in it.... mBean | |---getChildBean(i) |---setChildBean(cBean) | childBean | |---getXXX() |---setXXX() |---getXXX() |---setXXX() |---getXXX() |---setXXX() To be honest, Im very new to Expression language and JSTL...... I donno how to do it........
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
For starters, the c:forEach tag must have an array or collection to iterate over. So your mBean must have a way of exposing the child beans as something that it can digest. Frequently, this will take the form of an array, List, or Iterator. I'd suggest you start by adding a property to your mBean that exposes the children as such. So any of the following would work: whichever makes the most sense for your bean. This is the property that you would feed to the items attribute of the c:forEach action. Give it a try and post your code. Yeah, you may be a novice at JSTL, but you'll learn more if you write the code and get help on it than if someone just gives it to you.
|
 |
Vijay Vaddem
Ranch Hand
Joined: Feb 13, 2004
Posts: 243
|
|
Hi again,. This is how i was trying to do........ <c:forEach items="${dataBean.size}" var="index" varStatus="resultLoopStatus"> <c:forEach items="${dataBean.childBean[index]}" var="cBean" varStatus="resultLoopStatus"> <tr> <td> <c ut value="${cBean.url}" escapeXml="false"/> <c ut value="${cBean.linkText}" escapeXml="false"/> <c ut value="${cBean.annotation}" escapeXml="false"/> </td> </tr> </c:forEach> </c:forEach> And i got the following Exception.... Exception Message: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach> Stack: org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:286) org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:250) org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:172) javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:262) org.apache.taglibs.standard.tag.el.core.ForEachTag.doStartTag(ForEachTag.java:105) ... .;.. ... ..
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
Firstly, when entering code, be sure to click the "disable smiles" checkbox. That way, your <c ut> tags will look less surprised. OK let's start with your outer loop: If you check your JSTL reference (you are using a good reference, aren't you?), you will see that the items attribute specifies the collection to iterate over. And yet, you have given it a size (presumably an int). Not surprising that it complains:
Don't know how to iterate over supplied "items" in <forEach>
since one can't iterate an int. Let's assume that you have placed your data bean as scoped variable dataBean, and, as I instructed in a previous reply, you have created a method getChildren that returns a List or array of ChildBeans. Then your forEach tag would look something like: Now, within the confines of the foreEach, each child is in-turn exposed as scoped variable child. Before proceeding you need to understand why and how. [ April 29, 2004: Message edited by: Bear Bibeault ]
|
 |
 |
|
|
subject: how to use c:forEach....
|
|
|