This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Problem retrieving arraylist data in jsp page.Help needed :) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem retrieving arraylist data in jsp page.Help needed :)" Watch "Problem retrieving arraylist data in jsp page.Help needed :)" New topic
Author

Problem retrieving arraylist data in jsp page.Help needed :)

Geo Joseph
Greenhorn

Joined: Nov 28, 2007
Posts: 8
Hello all,
Im stuck with a peculiar problem(atleast for me ). The situation is, In my jsp page i have to retrieve certain values. my jsp code is...

Where the req_code values that i get is from the form beans. Now the only problem is to retrieve the values of the 2nd arraylist viz list1. The 2nd arraylist contains certain float values which corresponds to the req_code values. At present i can only retrieve the values of req_code vertically....the current output is...
R12345 Balance:
the way i want it is..
R12345 Balance: 345.0
and so on....
Please help me on how to achieve this.
regards,
G.Joseph
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
<select name="req_code" size=1>
<%SelCloseReqForm scrobj=null;
ArrayList list=(ArrayList)request.getAttribute("list");
ArrayList list1= (ArrayList) request.getAttribute("list1");
Object f=request.getAttribute("f");
if(list1!=null && list1.size()>0){
Iterator itr1= list1.iterator();
for(Iterator itr=list.iterator();itr.hasNext())
{scrobj=(SelCloseReqForm)itr.next();
scrObj1=(SelCloseReqForm)itr1.next();
%>
<option value="" <%=scrobj.getReq_Code() %> "," "\">
<%=scrobj.getReq_Code() %> Balance : <%=scrObj1.getReq_Code(); </option> <%}}}//end if %>
</select>
Here i am assuming list and list1 will have equal size everytime.

see if it works for you or not
Geo Joseph
Greenhorn

Joined: Nov 28, 2007
Posts: 8
Originally posted by Patricia Samuel:
<select name="req_code" size=1>
<%SelCloseReqForm scrobj=null;
ArrayList list=(ArrayList)request.getAttribute("list");
ArrayList list1= (ArrayList) request.getAttribute("list1");
Object f=request.getAttribute("f");
if(list1!=null && list1.size()>0){
Iterator itr1= list1.iterator();
for(Iterator itr=list.iterator();itr.hasNext())
{scrobj=(SelCloseReqForm)itr.next();
scrObj1=(SelCloseReqForm)itr1.next();
%>
<option value="" <%=scrobj.getReq_Code() %> "," "\">
<%=scrobj.getReq_Code() %> Balance : <%=scrObj1.getReq_Code(); </option> <%}}}//end if %>
</select>
Here i am assuming list and list1 will have equal size everytime.

see if it works for you or not


Hiya Patricia,
Thanks a ton for your valuable inputs. I think i didnt convey my problem correctly. Ok, the problem here is that the value scrobj.getReq_Code is comming from a bean, whereas the value of list1 is not comming from any bean. I've managed to print the values the way i want, the only problem here is ive done it twice in the select option. What i've done is...

And obviously, the values are getting iterated twice here. How can i print the values once. P.S-- the values of list is from bean, and list1 is not.
the output that im getting is something like this...
R1234 Balance: 23.0
R3456 Balance: 45.0
R5453 Balance: 89.0
and again in option
R1234 Balance: 23.0
R3456 Balance: 45.0
R5453 Balance: 89.0

Hope you got my point...Phew that was some typing...
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
My suggestion would be that instead of trying to do this in your JSP, move this into your Action (or some other Java code) where you can build up a collection that looks like what you need for your JSP. I am picturing a class named ReportItem (or whatever) that contains two properties: code, and balance. In your Java code you could take the two lists and create a single list of ReportItem objects.

- Brent
Geo Joseph
Greenhorn

Joined: Nov 28, 2007
Posts: 8
Originally posted by Brent Sterling:
My suggestion would be that instead of trying to do this in your JSP, move this into your Action (or some other Java code) where you can build up a collection that looks like what you need for your JSP. I am picturing a class named ReportItem (or whatever) that contains two properties: code, and balance. In your Java code you could take the two lists and create a single list of ReportItem objects.

- Brent


Hi brent,
Thanks for the response. Can you please give me an example on how to do this. I'll be very greatfull. The thing is the value of the req_code is comming from my resultset where ive created the getter/setter methods. And the other item that ive stored in the arraylist is returning a float value. So im just confused on how to do this. Basically how to iterate through the 2 arraylist in one logic iterate or scriplet(if we can achieve it in one iterate function).
regards,
Geo
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
It would be hard for me to give you an example without knowing more about your data. How do you pair up the records in the two lists? Is it just by order (the first record in list 1 matches the first record in list 2, ...)? If that is the case then I assume the lists would be the same size. I would use a for loop with an integer index and use the List.get(index) method instead of trying to manage two iterators.

- Brent
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
List1 must be having list of prices.....there is no criteria to map prices with your R..values. if there is no criteria of mapping...I mean you just want to print balance with R values ignoring the order...Then do this
while(rs.next()){
scrObj.setReq_Code(rs.getString("Req_Code"));
list.add(srcObj);
}
and then in your business service or whereever you want write this
int index =0;
for(int i=0;i<list.size();i++){
SelCloseReqForm srcObj = (SelCloseReqForm)list.get(i);
//create field balance in your Form.
srcObj.setBalance(list1.get(index));
index++;
}
and now in your jsp.
for(Iterator itr=list.iterator();itr.hasNext()){
{scrobj=(SelCloseReqForm)itr.next();{%>
<option value="" <%=scrobj.getReq_Code() %> "," <%=srcObj.getBalance()% "\"> <%=scrobj.getReq_Code() %> Balance : <%=srcObj.getBalance()%> </option<%}%>
See if it makes sense for you.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
if something is not clear then let us know
Geo Joseph
Greenhorn

Joined: Nov 28, 2007
Posts: 8
Originally posted by Patricia Samuel:
if something is not clear then let us know


Hi'ya Patricia wooooo, you are great. This is exactly what i wanted...looked quite simple didnt it, but i wasted 2 days just thinking about it. What an idiot i am geez that was great. I want to thank specially you and also brent for your suggestions and advices. Thank you i truely appreciate this.
regards,
Geo Joseph
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem retrieving arraylist data in jsp page.Help needed :)
 
Similar Threads
List Duplicated
paging and sorting
How to set request attribute on JSP page
to display the value of the string in a jsp page
EL behaviour