• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

logic:iterate

 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible to iterate thru a Arraylist using logic:iterate.
I put this arraylist in request object and want to access this in JSP..
is it possible.. if so, pleae let me know hoe to achive it??
 
Bernardus Irmanto
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it is possible. I posted a reply to a similar question ( yashnoo iyo, subject : put arraylist in the session? ).
pls go the topic
rgds
beN
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and I have a service class, where actually, we shld fetch some data from database and put it in a Array list.. Right now, we hardcoded this...
teh code for this serveice class

and in the Execute method of the action class..


Now, I have to access this request object and diaply in a JSP using
logic:iterate..
please help me out on this..
[ October 08, 2003: Message edited by: M K Raman ]
[ October 08, 2003: Message edited by: M K Raman ]
 
Bernardus Irmanto
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
among the logic:iterate tags, there are 3 attributes which are needed to specify the collection to be iterated in the jsp page. They are :
1. Collection : which is a runtime expression that evaluates to a collection
2. name : which is the name of the jsp bean containing the collection to be iterated over
3. property : the name of the property of the jsp bean specified by name( point 2)
In all those 3 attributes, you need to have a jsp bean which has a collection member variable to be iterated over.
So, In your case, you cant put the arraylist directly to the request object. you can define a jsp bean, declaring one arraylist, fill the arraylist with the arraylist returned by your getDepartments method. The next step is put the jsp bean in the request object.
then in the jsp you can have logic:iterate tag
which will look like :
<logic:iterate name="depatrmentBean" property="departments" id="row" indexId="index" scope="request" >
where the departmentBean is your jsp bean(refered by the name attribute). In the the departmentBean you need to have an ArrayList named departments(refered by property attributed).
and then you can use html:text/bean:write to output each member variables.
<html:text property='<%= "department["+index+"].deptId" %>' >

you might be a bit confused. We have "departments" (see the name attribute)and department(see the property attribute of html:text tag). They represent two different things.
departments is the collection you want to iterate over, and the department, is the object contained in the arraylist.

hope this can help
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks beN
I tried implemetning your logic...
But, its not working for me.. Ineed to look into this again... I need to look into Form code again!!
[ October 08, 2003: Message edited by: M K Raman ]
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M K Raman; I am not quite sure what you are trying to print; but I assume it to be the descriptions on the bean; you might give this a try and let me know if it works.
<logic:iterate property="departments" id="row" scope="request" >
<bean:write name="row" property = "description"/>
</logic:iterate>
On the other hand if your intention was to print id as well as description then you might have to use this
<logic:iterate property="departments" id="row" scope="request" >
<bean:write name="row" property = "id"/>
<bean:write name="row" property = "description"/>
</logic:iterate>
Also; I am not sure why you have a non default constructor in your bean when you have setters that do the same thing for you.
 
Bernardus Irmanto
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Anselm,

I dont think the code will work.
the property attribute of the logic:iterate tag "must" have the jsp bean which is refered by the name attribute, associated with it.
rgds
beN
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right Irmanto;
Raman; please change the code to use name in place of property as shown below
<logic:iterate name="departments" id="row" scope="request" >
<bean:write name="row" property = "description"/>
</logic:iterate>
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
beN, Anselm..
With the help of beN's reply, yesterday, I was able to solve it and display it ..
Thanks beN for looking into this...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic