aspose file tools*
The moose likes JSP and the fly likes <c:forEach> to iterate a collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "<c:forEach> to iterate a collection" Watch "<c:forEach> to iterate a collection" New topic
Author

<c:forEach> to iterate a collection

amarshi mohanty
Ranch Hand

Joined: Jul 01, 2008
Posts: 110
Hi All,

//in java
Collection coll = new ArrayList();
coll.add(crs.getString(1));
coll.add(crs.getString(2));
request.setAttribute("coll ",coll );

where crs is the cachedrowset object.

//in jsp
<c:forEach items="${coll }" var="nameObj" varStatus="record">
<tr>
<td><cut value="${nameObj.var1}"></cut></td>
<td><cut value="${nameObj.var2}"></cut></td>

</tr>

I am getting the following Exception:

javax.servlet.jsp.JspException: An error occurred while evaluating custom action
attribute "value" with value "${nameObj.var1}": Unable to find a value for "var1
" in object of class "java.lang.String" using operator "." (null)



Now my question is how will the coll obj know that the values added are var1 and var2.
because even if i am putting all the values to coll obj i dont hav to display all .So somhow i need to access them using the var1 and var2.

If i will directly give:
<cut value="${coll }"></cut>

Then this prints all the vales with comma separated.How can i segregate that 1sr record will have values as var1 and var2,then 2nd record will hav its var1 and var2,etc...


Thanks
Amarshi


SCJP (97%), SCWCD (95%))
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
The exception is descriptive to indicate where you are going wrong,

javax.servlet.jsp.JspException: An error occurred while evaluating custom action
attribute "value" with value "${nameObj.var1}": Unable to find a value for "var1
" in object of class "java.lang.String" using operator "." (null)


Though I don't understand your question fully, I think you need to create a List of javabeans (with var1 and var2 as you say)
instead of creating a list of strings.

Here is how this can be done.

1. Create a class with private instance variables var1 and var2 and generate getter,setters for these variables. (To put it simply, have a javabean created )
2. While iterating through the resultset ,create a javabean object for each row (call the setVar1 and setVar2 methods for each column) and add this bean to your list
3. Set the list in your request attribute
4. Forward this your JSP
5. Use the <c:forEach> tag to generate the HTML fragment.

Thanks,
Srikkanth
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're just iterating over a collection of strings--not objects. As the previous poster suggested, either create a better data transfer object (DTO) that names individual components, or just understand that you're iterating over a collection of strings and write the JSP code accordingly.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 253

Hi amarshi ,


javax.servlet.jsp.JspException: An error occurred while evaluating custom action
attribute "value" with value "${nameObj.var1}": Unable to find a value for "var1
" in object of class "java.lang.String" using operator "." (null)


Now my question is how will the coll obj know that the values added are var1 and var2.


I believe, you have added the var1 and var2 values in coll and these are of String type. To access these strings only ${nameObj} is enough. When you try ${nameObj.var1} container thinks nameObj to be a bean and tries to find getVar1() method. As String class does not have this method you would get the error.

You can access these values using loop in the following way --


while running this jsp you would get the out put as follows -

The value of attribute is = [var1, var2]
The way to access using indes is = var1 and var2
The values got from looping is as follows - var1 var2

I hope this would clear your doubt


Regards
Salil Verma
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: <c:forEach> to iterate a collection