File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes ArrayList and Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "ArrayList and Ajax" Watch "ArrayList and Ajax" New topic
Author

ArrayList and Ajax

paresh doshi
Ranch Hand

Joined: Nov 20, 2006
Posts: 81
Is it possible to pass arraylist from Struts Action to JSP page.

The execute() of Struts Action is called using Ajax.


Thanks.<br />Paresh Doshi<br />SCJP 1.4
Johnson Abraham
Greenhorn

Joined: May 31, 2004
Posts: 19
As far as i know you cannot pass an ArrayList from your struts application to AJAX. You can set the Arraylist in request Object but only JSTL or Scriptlets would be able to identify it.

What you can do is build an XML object using your Arraylist and write the xml stream to the response. This way Ajax will be able to read the xml stream using request.responseXML.

Thanks
johnson
Karthikeyan Ramaswamy
Ranch Hand

Joined: Apr 30, 2008
Posts: 41
hi,

i don't know about your application,
i thought it's possible using request or session Attributes.

session/request.setAttribute() in your Action,
session/request.getAttribute() in your JSP.

lets try this,if still you got a problem post your code.
------------------
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I think when you use ajax, sessions won't work, you have to redirect to a different page to request the session variable.

when you use ajax, your not redirecting to a page, so that server page will not be able to 'see' that session at all.

As far as I know anyways. Inform me if I am wrong though.

Justin Fox


You down with OOP? Yeah you know me!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Originally posted by Justin Fox:
I think when you use ajax, sessions won't work ... Inform me if I am wrong though.

You are wrong. Sessions work just fine in Ajax requests.

To pass an ArrayList (or any other ordered construct) as an Ajax response, you can specify a JavaScript array using JSON.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 118

Hi Bear,
Im also trying to send an arraylist object in request or session, using ajax call struts action is called. Just now i gone through your thread,
can you provide any sample code for how to set an array object in session and obtain it in jsp using ajax call.

As for as now ajax call is made to struts action and im in printwriter im printing my result and in javascript by using xmlhttp.responseText(), im getting it and using in jsp page.
In my application, im having select box, in it onchange event im calling ajax call and i have to return a array list and by using the list i have to paint a table using Display tag..

Please help me out in this...
Looking forward for your reply......

Please provide me some sample code....

Thanks and regards,
Prabhu.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Why is your post in a quote? And in the future, it'd be best to start new topics for your own questions rather than reviving older topics.
Im also trying to send an arraylist object in request or session, using ajax call struts action is called. Just now i gone through your thread,
can you provide any sample code for how to set an array object in session and obtain it in jsp using ajax call.
Since JavaScript has no ArrayList, you need to use a JavaScript array, And since you can only send text as a response, you can't send it directly. The easiest way to send the data is to use an interchange format known as JSON, which looks like JavaScript literals.

JSON is easy to transform into JavaScript data using the eval() method.

That said, these days it's madness to write your own raw Ajax, so I always recommend employing a library to help you. I'm using jQuery these days and its $.getJSON() method makes it super easy to grab JSON from the server and automatically digest it for you.

In my application, im having select box, in it onchange event im calling ajax call and i have to return a array list and by using the list i have to paint a table using Display tag..
If "display tag" is a JSP action (tag) you are not going to be able to do that. Ajax is a JavaScript mechanism. You cannot cause JSP on a page to re-evaluate using Ajax. The page executed before it was sent to the browser and only a new request can cause a re-execution. That's the whole point of Ajax -- to be able to make requests without having to repaint the entire page.

You can use JSP to format HTML using JSP to send back as the Ajax response, but that's not what you said you wanted to do.
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 118


Hi Bear,

Thank you for your info. I will post a new thread for my future post.

As you said, by using JSON we can get the array which is obtained using our struts action, in our jsp..... am i right...?
As you said by by using 'EVAL' function.....
I do not know much about JSON.... but i will read about it...But for now... im having an array list in my struts action.... and how to set it in session and obtain it in jsp....

Please provide me the solution for this....

in your older post you have said that, we can obtain a session in our jsp page which is set in our action class, by using JSON.....

I need sample code for that.....
It would be helpful for me, but i wont stop here and go through JSON and know about it... its sure.... Mr.Bear...

Looking forward for your reply...

Regards,
Prabhu.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Please stop putting your responses in quotes. It makes responding to your posts difficult. Quotes are used to quote passages from previous posts, not for new material.
[ July 06, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

for now... im having an array list in my struts action.... and how to set it in session and obtain it in jsp....
No differently than when not using Ajax. From the server point of view there is no difference between a full-page request and an Ajax request. You can use the same server-side mechanisms regardless of how the request was initiated.

in your older post you have said that, we can obtain a session in our jsp page which is set in our action class, by using JSON.....
No, I didn't say that. I said that the session is available to server-side resources regardless of whether Ajax is being used or not.
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 118
Hi Bear,

I can get you bear;

Please is it possible for you to provide me a sample code, in which an ajax call is made to struts action and in action class we retrieve an arraylist and set it in session and in javascript by using xmhttp.responseText, i should be able to obtain that arraylist in my jsp page....

Please bear, spend for me some few minutes and provide me this code using JSON javascript array.... im at the eve of my project schedule......

Looking forward for your reply.....

Thanks and Regards,
Prabhu.
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 118
Hi Bear,
I have posted a new thread in this forum....

Please have a look at it and im seeking your help in that...

Regards,
Prabhu.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Creating a JSON array is simple. Let's say you had an array of strings; the format would be:

If this is returned as the response of the Ajax request, then a statement such as:
will evaluate it.

But, much easier to use a library such as jQuery which will handle the conversion for you.
[ July 07, 2008: Message edited by: Bear Bibeault ]
 
wood burning stoves
 
subject: ArrayList and Ajax
 
Similar Threads
Ajax - Struts - JSP
Struts 2 : Ajax support question
AJAX-JQuery-Struts getJSON function data issue
Workflow and actions
good book for ajax