Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<c:forEach> to iterate a collection

 
amarshi mohanty
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 240
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic