struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank W. Zammetti" <>
Subject Re: Ajax and Struts
Date Sun, 03 Sep 2006 00:10:09 GMT
Wen-Jung Chen wrote:
> Hi,
> I have a scenario that user enters data in the text box in the browser and
> clicks Continue button. I send this value into Struts Action class which
> checks against database to return me the true or false value. Based on this
> true or false value, I will display or hide <div> in the same page.
> I am using synchornous Ajax in Javascript to call Struts Action class. In
> Struts action class I set the status in Session attribute. Once Ajax call
> returns, I can get the status in javascript and display or hide <div>
> accordingly.

Umm, how exactly are you accessing the session object from Javascript? 
That's a neat trick! ;) LOL

> Is there any way to use request attribute instead of session attribute for
> this?

Generally speaking, if your hand-coding your AJAX (and even if you 
weren't this might still be true), you would just forward to a JSP from 
your Action, same as always, and render some suitable response that you 
then handle in your client.  In that case, you can put everything in 
request scope and render the page using the usual bag of tricks (i.e., 
JSTL, Struts tags, whatever).  In your case, the JSP, in its entirety, 
may literally just be:


...where the value of displayOrNot is "true" or "false" (whether it's a 
String or a Boolean, your choice).  Then the client just does:

if (XMLHttpRequestInstance.responseText == "true") {
   // Show DIV
} else {
   // Hide DIV

> Or should I use JSON RPC to do this?

It might make things easier, yes.  Unless you have a specific need, I 
for one recommend using a good library to avoid the intricacies of AJAX 
(which can sneak up and bite you if you aren't careful).  Specifically, 
I'd take a look at DWR as well... very similar conceptually to JSON RPC, 
but I suspect more mature and widely used.  Note though that you 
probably won't be calling a Struts Action with either, and even if you 
do, it won't be through the normal request processing cycle, so consider 
if that's what you really need to do.  If you do need the whole request 
processing cycle, you might also have a look at AjaxParts Taglib (APT) 
in Java Web Parts (JWP):

Directly relevant link:

These docs will give you all the details, I'd suggest after you read 
them that you grab the JWP sample app to see it in action.  APT has the 
benefit of eliminating all your client-side code (and in your case 
specifically, it's even a trivial exercise).  There are of course plenty 
of other good libraries to choose from, Dojo and Prototype float to the 
top pretty quickly, but both will still require you to hand-code 
Javascript, APT lets you avoid that entirely (usually).

> Thanks,
> Wen-Jung


Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
AIM/Yahoo: fzammetti
Author of "Practical Ajax Projects With Java Technology"
  (2006, Apress, ISBN 1-59059-695-1)
Java Web Parts -
  Supplying the wheel, so you don't have to reinvent it!

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message