I'm a big fan of DWR as well, it's a very worthy alternative.
But, I don't mind a little self-promotion, so... how does this strike
you? ...
<ajaxConfig>
<group ajaxRef="MyForm" form="compose_mail_form">
<element ajaxRef="SubmitButton">
<requestHandler type="std:QueryString" target="/process_new_mail.do">
<parameter>to=to</parameter>
<responseHandler" type="std:Alerter">
<parameter />
</element>
</group>
</ajaxConfig>
Then, in your JSP:
<html:form styleId="compose_mail_form" method="post"
action="process_new_mail" enctype="multipart/form-data" focus="to">
<label for='to' <logic:messagesPresent
property="to">class='error'</logic:messagesPresent>>To:</label>
<html:text property="to" styleClass="text" style="width: 25%;" />
<br class="clear" />
...
//more form elements
...
<a href="#">SUBMIT</a><ajax:event ajaxRef="MyForm/SubmitButton" />
</html:form>
<ajax:enable />
This would result in some Action, mapped to process_new_mail.do being
executed, and you would get a single parameter submitted, "to", using
the value of the form field "to". The response from the server,
whatever it is, would be displayed via alert().
This is what AjaxParts Taglib (APT) offers... you define an event in a
config file, some user-initiated (usually) event that fires an AJAX
request. For each event, you define a request handler, which forms the
request to the server... there are a number of "standard" handlers, for
instance, if you want to construct XML from your form, that's standard.
You also define one (or more) response handlers, which is something
that happens when the response comes back. Again, there are a number of
standard handlers, like Alerter... there is also things like InnerHTML
(populate a page element by updating innerHTML), stdXSLT (transform XML
response via XSLT on client), and much more.
Note that you didn't have to right ANY Javascript whatsoever! And the
changes to your JSP amounts to adding an <ajax:event> tag to any element
that will fire an AJAX event, and the <ajax:enable> tag at the end (plus
the taglib declaration of course). Changing the AJAX functionality is
as easy as modifying the config file, you wouldn't need to touch your
JSP again! And, should you need to do more advanced things that the
standard handlers don't cover (they should do the job probably 80% of
the time or better though), there is a pretty simple mechanism for
writing your own custom handlers, which you can then use just like the
standard handlers.
If the no coding approach sounds good, check it out further:
http://javawebparts.sourceforge.net/
I suggest clicking the Javadocs link and going to the first package
listed, javawebparts.ajaxparts.taglib... all the details can be found
there... then, download JWP and check out the sample app for all sorts
of examples of APT usage.
Shill time over :)
Frank
draegoon Z wrote:
Hey,
I use DWR + dojo to use AJAX with struts:
http://getahead.ltd.uk/dwr/
http://dojotoolkit.com/
Although dojo has some flashy widgets and stuff, its power is the
dojo.io.bind() function:
http://dojotoolkit.org/docs/intro_to_dojo_io.html
Here is an example of using Struts Actions with bind():
<html:form styleId="compose_mail_form" method="post"
action="process_new_mail" enctype="multipart/form-data" focus="to">
<label for='to' <logic:messagesPresent
property="to">class='error'</logic:messagesPresent>>To:</label>
<html:text property="to" styleClass="text" style="width: 25%;" />
<br class="clear" />
...
//more form elements
...
<a href="#" onclick="submitNewMail();">SUBMIT</a>
</html:form>
NOTE: form has been given a 'styleId' and using <logic:messagesPresent>
to display errors in form
function submitNewMail(){
//validateNewMail(form);
var bindArgs = {
url: "<html:rewrite action="process_new_mail" />",
error: function(type, data, evt){
alert("An error occurred submitting new mail: " + data);
},
load: function(type, data, evt){
DWRUtil.setValue("social_mail_right_con", data); /*
setValue doesn't execute javascript! */
document.getElementById('social_popup_layer_container').innerHTML=document.getElementById('ajax_hidden_helper').innerHTML;
popup('social_popup_layer_container',true);
},
mimetype: "text/html",
formNode: document.getElementById("compose_mail_form")
};
dojo.io.bind(bindArgs);
}
NOTE:
1) the use of <html:rewrite /> for the 'url'
2) formNode is the 'styleId' of your <html:form />
3) the stuff you see in the load function is application specific. It is
how I display <html:errors />
and <html:messages /> in a backwards-compatible manner for AJAX. I
basically force
the reloading of a JSP (tile) that has the code: <html:errors /> and
relevant logic.
Ok, now for the good stuff:
1) Yes, your ActionForm will work as usual. If it is invalid, errors are
displayed. If it validates, it goes
on to the Action.
2) The action MUST RETURN "NULL", otherwise the page will reload to your
<forward>
3) You write your output to the response object in your action. Remember
when it was just servlets?
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
//Make ActionMessages for business logic errors...
ActionMessages messages = new ActionMessages();
HttpSession session = request.getSession();
MyForm f = (MyForm)form;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("ACTION_SUCCESS:Message Sent!");
out.close();
//return(mapping.findForward("success")); NOT AJAX WAY!
return(null);
}
There are many ways to handle the repsonse stuff with AJAX + STRUTS. Use
your imagination.
In this instance I just have a javascript function parse the output. You
could return HTML and use
innerHTML(). You could parse a string like: "FORWARD:my_action_name".
You could have: "JAVASCRIPT-FUNCTION:handleSuccess()".
I often use dojo.io.bind() in conjunction with DWR.setValue():
http://getahead.ltd.uk/dwr/browser/util/setvalue
or using DWR to forward to a JSP:
http://getahead.ltd.uk/dwr/examples/text
Bottom line is that javascript will handle the forwarding instead of you
struts config.
If some has a way to do it with the config, I'd love to hear from you.
BTW - I took the time to write this because after all the reading and
searching I did,
I still didn't have a clue how to use all this stuff together. I hope
this helps everyone.
-Joe
-----------------------------------------------------------------------------------
WEB DESIGN BY DRAEGOONZ
Joseph "DraegoonZ" McGranaghan
http://www.draegoonZ.com
603-620-0854
[EMAIL PROTECTED]
From: chamal desilva <[EMAIL PROTECTED]>
Reply-To: "Struts Users Mailing List" <user@struts.apache.org>
To: user@struts.apache.org
Subject: Struts and AJAX
Date: Sun, 2 Jul 2006 20:57:52 -0700 (PDT)
Hi,
Can I use struts with AJAX. Does struts has classes
and tags for AJAX or do I have write my own code.
Thanking You,
Chamal.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: [EMAIL PROTECTED]
Java Web Parts -
http://javawebparts.sourceforge.net
Supplying the wheel, so you don't have to reinvent it!
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]