Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

<c:forEach> to iterate a collection

 
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><c:out value="${nameObj.var1}"></c:out></td>
<td><c:out value="${nameObj.var2}"></c:out></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:
<c:out value="${coll }"></c:out>

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
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Ranch Hand
Posts: 243
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
 
Author
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.
 
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
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic