Easy Ajax with Struts 2

by Paul Browne

A while back , I wrote an article for O'Reilly's sister site , Java.net about How to add Ajax to your Struts Application. While it was pretty straightfoward (as is Ajax underneath all the hype), it still involved a little to much JavaScript for my liking. Nothing against JavaScript, but if you write it , you have to support it. Far better to use a standard Ajax library , like DWR , Prototype or Dojo.

So , along comes Struts 2. Be careful - it's good , but a different beast under the covers from Struts 1. It does a lot of things better - for example , the way it has Actions as normal POJO's makes it a lot of things easier to unit test.

Struts 2 also gives you Ajax 'out of the box'. No writing of javascript, no debugging against various browsers; just configure and go. A full description of how to setup Struts 2 to use Ajax is in this wiki article. The quick version is:

1. Setup Struts 2
2. Add the Struts-Ajax URL and Div Tag.

That's it. Ajax without the fuss.

Even better that the Struts guys have implemented the Ajax functionality using the Prototype Ajax Library, so you get the benefit of all the improvements there.

What do you think? Will you move to Struts 2 just for the new Ajax capabilities?


Guy Mac
2007-06-29 16:00:55
Good to know. I like prototype, and have been looking to move to Struts 2, so probably will now.
2007-06-29 20:34:40
S2.0 Ajax is implemented with Dojo, not Prototype. S2.1's Ajax support has been moved to a plugin which will allow easier wrapping of different Ajax implementations.
Paul Browne
2007-06-30 03:06:53

Apologies for any confusion over Dojo and Prototype. Struts 2 is easy enough to configure to use either, so you can choose your favourite one.


2007-07-01 20:15:21
How do you configure S2.0 to "use" prototype and what do you mean by "configure"? AFAIK all of the S2 tags use Dojo; using Prototype would require completely redoing the Ajax theme. Have you done this?
Paul Browne
2007-07-02 06:49:33

You're correct in that all of that the Struts 2 tags (S2) use Dojo.
Using Prototype instead is easy:

You use the existing Struts 2 (non Ajax) tags, Struts.xml and Struts Actions with no change to the code. Add Prototype to the page via the standard include. All S2 tags render as HTML, so these can fire (javascript) events that Prototype acts upon. Prototype requests information from a (Struts) based url, which returns the contents of a JSP page, for Prototype to render via DHTML.

No changes to Struts2 or prototype code, just standard configuration; only code added is to implement your application's logic.

Struts 2 need not be aware that the incoming request is from Prototype, nor is Prototype aware that it is actually Struts 2 serving up the response to it's Ajax calls.


2007-07-02 08:08:51
That's quite a bit different from what you said in the article, and that isn't the same as S2 "using" Prototype, IMO. That's using Prototype in an S2 app, and as you said, that has absolutely nothing to do with S2.

It's also, then, no longer a matter of just using and tags. You might want to update the article.

Othon Reyes
2007-07-26 10:06:21
I'm newbie with dojo, prototype and struts2 but i have been reading several forums about struts 2 and its bad performance and it's even worse when struts2 use dojo (i mean when you use the ajax tags or the ajax theme).
I think i will use struts 2 with prototype, i have found some information that prototype is more stable and easy than dojo.
Dojo you have an idea why the people of struts 2 choose dojo instead of prototype?. I have been thinking about this since i read that struts2 use dojo. why?, prototype is more popular.
Paul Browne
2007-07-27 06:15:33
Othon ,

Sorry , no particular words of wisdom !

All I know is that you can choose to use either.


2007-08-01 19:00:29
@Othon: Dojo was chosen because it's a UI library, not just a JavaScript library. I'm not sure what Dojo performance issues you're referring to other than Dojo being fairly large-ish; Ajax requests themselves are quite acceptable.

Don't think that you just set a flag and you're suddenly using Prototype; the S2 tags *only* use Dojo. You may use Prototype *manually* (just like you could manually use mootools or any other JavaScript library) but you'll lose the custom tag support unless you create your own theme / FreeMarker templates / etc.

2007-10-16 03:40:39
Good to know about these features. I too need to move from Struts 1.x to Struts 2.x.

Can't I use Dojo with my Struts 1.x apps?