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]

Reply via email to