Thursday, November 3rd, 2005

Struts Ajaxian Web Applications

Category: Articles, Java

<p>

Struts is the grand-daddy of Java web frameworks, and although the buzz has moved on to other Java web frameworks like JavaServer Faces, WebWork, Tapestry, … a lot of people are still writing Struts applications.

Paul Browne has written an introductory article called Sprinkle Some AJAX Magic in Your Struts Web Application.

The article focuses on the Ajax client side, rather than integration with Struts.

Designing AJAX into Your Application

The JavaScript outlined above can cope with the way Struts is used in most applications, including those that are much more complex than our simple example. However, you may find that following the points below makes it easier to write and use your code:

  • To avoid duplicated code, it can often be better to use the same Struts Action and JSP for the initial request (i.e., show full page) and the AJAX (update part of page) requests.
  • Within the common Action (controller) class, decide which sections of the JSP page (all of the JSP or only part of it) need to be sent to the browser. By setting flags in either the web server session or ActionForm, the JSP page knows which sections need to be rendered.
  • Within the JSP, use Struts or JSTL tags to decide if we need to render a section of HTML or not.

An updated version of this project, with AJAX enabled, can be downloaded here: struts-Ajax.zip

Related Content:

Posted by Dion Almaer at 2:53 pm
3 Comments

+++--
3.4 rating from 10 votes

3 Comments »

Comments feed

This article really doesn’t deserve some publicity, as what it presents is a very primitive and buggy approach to Ajax.
http://www.anyware-tech.com/blogs/sylvain/archives/000222.html

Comment by Sylvain Wallez — November 3, 2005

> To avoid duplicated code, it can often be
> better to use the same Struts Action and JSP
> for the initial request (i.e., show full page)
> and the AJAX (update part of page) requests.

This sounds like that “same action class” will have a fair amount of conditionals. Ouch.

(I’d fully quote the other two “high points” about using JSP and JSTL to “conditionally” decide whats rendered, but lets keep this short..)

Fwiw, I recently retrofitted AJAX mechanisms into a struts app with complex forms that were designed w/ JSP/JSTL. Not pretty.

JSTL “conditional” display of the page just gets in the way. E.g. when the ajax comes along its possible that under certain cases content will simply be missing since this is the “JSTL way”.

Instead, I’d recommend rendering the page initially with all the html, but if there are portions to be “hidden”, hide them with div-visibility-false, or empty div elements.

The ajax server side should be small actions that do just enough, and no more. These can utilize shared classes from the biz logic.

I’ve had the best result returning javascript-in-a-jsp for the ajax response, and eval-ing it in the response handler on the client side.

In some cases, its useful to return a snippet of JSP-generated-html. This can then be set in a div/span with innerHtml.

jeff

Comment by jeff — November 7, 2005

As the author of this article , thanks for the link!

Not going to reply to the first commenter, as recommendations as when to use/ not use the techniques are pretty clearly set out in the article :-)

The 2nd Comment is correct – the Action class does get very busy. However, instead of a lot of ‘if…then’ statements , I’d recommend a command pattern (something that the guys from Struts have now made a framework for).

From other comments I recieved, most (Enterprise Java) people are coming from an ‘I Know Struts, but I don’t know Ajax’ point of view. As they begin to use Ajax on their 2 or 3rd project, they tend to a ‘purpose built’ framework like DWR , especially if the project is a new build.

Comment by Paul Browne — January 16, 2006

Leave a comment

You must be logged in to post a comment.