struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "draegoon Z" <>
Subject RE: Struts and AJAX
Date Mon, 03 Jul 2006 17:08:31 GMT

I use DWR + dojo to use AJAX with struts:

Although dojo has some flashy widgets and stuff, its power is the function:

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 
<html:text property="to" styleClass="text" style="width: 25%;" />
<br class="clear" />

//more form elements

<a href="#" onclick="submitNewMail();">SUBMIT</a>

NOTE: form has been given a 'styleId' and using <logic:messagesPresent> to 
display errors in form

function submitNewMail(){


        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! */

        mimetype: "text/html",
        formNode: document.getElementById("compose_mail_form")


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 

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;

	PrintWriter out = response.getWriter();

	out.print("ACTION_SUCCESS:Message Sent!");

	//return(mapping.findForward("success"));	NOT AJAX WAY!


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 in conjunction with DWR.setValue():

or using DWR to forward to a JSP:

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.




Joseph "DraegoonZ" McGranaghan


>From: chamal desilva <>
>Reply-To: "Struts Users Mailing List" <>
>Subject: Struts and AJAX
>Date: Sun, 2 Jul 2006 20:57:52 -0700 (PDT)
>Can I use struts with AJAX. Does struts has classes
>and tags for AJAX or do I have write my own code.
>Thanking You,
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around
>To unsubscribe, e-mail:
>For additional commands, e-mail:

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

View raw message