• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

AJAX and Struts

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm currently trying to code several basic behaviours in a struts based web app using AJAX technics.
It works fine and javascript actions such as updating part of a page without a whole page reload are awesome !

But, as soon as I deal with Form data, I'm facing some difficulties.
Since my javascript methods do not Post the Form, I cannot handle user's data so i'm forced to code extra stuff as walkaround in order not to loose data. It actually works but doesn't look good to me at all.

Do you guys know some good AJAX technics for a better integration with the Struts framework ?

Hope I'm clear enough.
If not, let me know
:-)

Thanx
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the XmlHttpRequest to send a HTTP POST request. You set the HTTP Method to POST in the open(...) call, and then pass the form data into the send() call.




Notes:
- From your post, I'm not sure if you're familiar with the HTTP protocol. Glance over RFC 2616 if you don't know exactly what sets GET and POST requests apart. See http://www.w3.org/Protocols/rfc2616/rfc2616.html

- The string passed into the send method look just like a query string in a HTTP GET: it's "var=value&var=value&var=value".

- The example above would work for an actionform containing two fields: foo & baz.

- You have to url-encode the request contents. I probably got the capitalization wrong in the example. You may have to hit google for a bit.

- A "live HTTP headers" tool is invaluable in debugging these things. There's an extension for Firefox, and a seperate program for Internet Explorer. Of the latter, I don't remember what it's called, but it's often referred to on ieblog.

(edit)

- You may have to set some request headers before calling the send(...) method. See note #1.
[ November 15, 2005: Message edited by: Barend Garvelink ]
 
Author
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Struts actually works very well with Ajax techniques, as long as you play by Struts' rules.

Struts will automatically populate an ActionForm from the query appended to an Ajax request. For example, if the URL of your Ajax request is ajaxRequest.do, you can append a query string like this:

ajaxRequest.do?parm1=hello&parm2=goodbye

If you have an ActionForm with properties parm1 and parm2, they will be automatically populated with the values hello and goodbye, respectively. The Action can then process the request normally and then return a response.

The Taconite framework (taconite.sf.net) integrates very well with Struts because it will automatically build the query string for you.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Ryan that integrating AJAX and Struts is fairly straightforward. Make sure you post to the right action and set the right content type inside the run method of the action. You can have multiple functions in the AJAX JSP, just make sure to pass them to the AJAX action.

Here is one example:


[ December 10, 2005: Message edited by: Levent Gurses ]
reply
    Bookmark Topic Watch Topic
  • New Topic