• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

iterate an object in struts-2

 
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an ArrayList in a javaclass and I want to iterate that in jsp...

How to achieve this?

The arrayList should be in the action class related to that jsp,isn't it,for the framework to to iterate the arrayList?
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should do: Iterator
This should help: struts-2-ognl-expression-language-example-1

try (untested):


unsure how you actually extract the counter variable and index the list, OGNL must be it though.
The Iterator link gives you a hint on some OGNL magic:

Let me know if it worked.
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now this is what I have...





SO my Iterator will look like this...




???--- there should be a td for each element of the bean? how is that achieved ?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pradeep, first of all you've shown us incomplete code, your code has two starting iterator tags but only one of them is closed.

Actually the second iterator is not needed, you just need the var attribute of iterator tag. The iterator needs to iterate over the ArrayList and inside that, you need to put multiple <td> elements with s:property tags within it. Something like this
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tudor Andrei Raneti wrote:


That's not legal JSP...
 
Tudor Raneti
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, Eclipse sais about end and begin attributes that they are undefined.
I copy pasted the example from struts-2 documentation (since he asked for struts 2 as he sais in title):
http://struts.apache.org/2.1.8.1/docs/iterator.html
and only added the <s:property tag

A loop that iterates 5 times

That's why i also said "try (untested):". Is struts 2 documentation missleading?

my result was on using that: Attribute begin invalid for tag iterator according to TLD
Anyway it would've been nice to see it work like that...
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's no nested JSP tags in the examples. The begin/end attributes were added sometime around 2.1.6 or so (I don't recall for sure). If you're not using a version that includes those attributes it won't work.
 
Tudor Raneti
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using struts 2.1.8.1 (which is the latest)
And this is the struts 2 documentation alright:

libraries in eclipse:

They still don't work...
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Struts 2.0.11 and, there is no thing as 'var',so I presume it as; id='res'.

Now, I write like this

<s:iterator value="rses" status="pos" id="res">
<tr>
<td><s:property value="res.rsId" /></td>
<td><s:property value="res.category" /></td>
<td><s:property value="res.rulesetName" /></td>
<td><s:property value="res.descripion" /></td>
<td><s:property value="res.author" /></td>
<td><s:property value="res.fromDate" /></td>
<td><s:property value="res.toDate" /></td>
<td><s:property value="res.rsPid" /></td>
</tr>
</s:iterator>


but it doesn't display the records... is id incorrect?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure there are records in your database?? And since rses is private in your action, do you have public getter/setter methods for rses property in your action??
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it compulsary to have the ArrayLists only in the Action ? can't I have them in any class?
Will the framework look for the Arraylist name present in the Iterator only in Action class and not in other classes?
I have my 'rses' in the class that is taking data from DB and if the aforementioned is true then no wonder I don't see any records!
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is it compulsary to have the ArrayLists only in the Action ? can't I have them in any class?


What do you mean by "any class". Struts only accesses properties of your action class. Action class doesn't necessarily mean sub-class of ActionSupport, it can be any POJO which is declared as a action in the struts.xml config file. So basically if rses is a property of your action which is serving the request, then the iterator will work. If you want to manually put rses as an attribute in any scope, then you can use the #<scope> syntax of OGNL...
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it's very clear but still not populating values though...

yes there is data in DB and the get,set are public!

Wonder what else could be the culprit!

Also I tried in a seperate file like this... on success directed to this jsp



It doesn't print anything! how to track this?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the actual name of the property in your action?? Is it rses or rsList?? Put a log or System.out.println statement in your while loop to see if the list actually contains elements i.e. whatever the query you use to get results from the database, actually return any value or not...
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is rsList and its properly displaying the values ...

Here it is ...




Here's the output... There is only 1 record in DB...

------------------------------------------------------------
The rsIS is :--> 1
The rsCat is :--> wef
The rsName is :--> Rs1
The rsDesc is :--> rg
The rsAuthor is :--> 3r5g
The rsFrom is :--> 2009-12-14
The rsTo is :--> 2009-12-28
The rspid is :--> 1
-------------------------------------------------------------
------------ ONE RULESET OBJECT ADDED ---------
The rses size is :--> 1




The while loop is in method getRSDataForProject() residing in a seperate class... This method is called from the action class as shown below!

In the action class...




output:-

----> size of rsList is :--> 1

I hope the Iterator is fine!

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The while loop is in method getRSDataForProject() residing in a seperate class


So basically getRSDataForProject method is in DBToRuleSetPersistence class?? Does this method return the ArrayList or not?? As I can see, there seems to be a getRses method in the DBToRuleSetPersistence class which actually returns the ArrayList. So does the getRSDataForProject method actually sets the ArrayList to a static field which is returned by the getRses method?? If that's the case then this might create concurrency problems. Other than that there doesn't seem to be any problem with the code. The iterator should work...
 
Tudor Raneti
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If nothing works, <c:foreach> is there.
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No,that method does not return anything!


This is the rses ArrayList in DBToRuleSetPersistence class.





but in action class the ArrayList is





is there a problem??

I can see that it's adding the row( very much compressed than usual size) but nothing in it!

 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no clue how your code is working. In the code that you showed, getRses is an instance method. But in the previous code, you called getRses as if it were a static method (21. DBToRuleSetPersistence.getRses()). I don't even know what this line means

I can see that it's adding the row( very much compressed than usual size) but nothing in it!


Adding the row where?? Print the values in the ArrayList in your action class i.e. after you call rsList=DBToRuleSetPersistence.getRses(); to see if there are actually any elements in the list. Also when your JSP is executed, see if there's any exception that is coming (although there's no reason for an exception, still there's no harm in checking)...
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding the row, meaning I am iterating a <TR> and I could see a small,compressed row adding to the header of table...

yeah the values in rsList are fine..here it is...






output:-
---------


The 1 is :--> 1
The 2 is :--> wef
The 3 is :--> Rs1
The 4 is :--> rg
The 5 is :--> 3r5g
The 6 is :--> 2009-12-14
The 7 is :--> 2009-12-28
The 8 is :--> 1


Still I see no values...

This is frustratimg!
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using a "#" character in the OGNL inside the iterator, so if the "id" attribute is "res", use "#res.theProperty" in the property tags (where "theProperty" is a property of the object). Alternatively, you can just use "theProperty" by itself, since each object will be put on the top of the stack.
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep the '#' works.i think OGNL is complete with a '#'


Thanks Ankit,David and co...
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Action class should have getter and setters for the list

ie




In the jsp you need to do the following...

No need to use a prefix of list name for each property...


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
<s:iterator value="rsList" status="pos" id="res">
<h4><s:property value="rsId" /></h4>
<h4><s:property value="category" /></h4>
<h4><s:property value="rulesetName" /></h4>
<h4><s:property value="descripion" /></h4>
<h4><s:property value="author" /></h4>
<h4><s:property value="fromDate" /></h4>
<h4><s:property value="toDate" /></h4>
</s:iterator>
</body>
</html>


Hope it is helpful to you...

Maki Jav
 
Pradeep Adibatla
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep! As david said eother with hash or directly the variablename of the object... thanks!
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic