struts2动作和jquery ajax [重复]

This question already has answers here:
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2012-09-20 11:40:29Z" class="relativetime">7 years ago</span>.</div>
        </div>
    </aside>

Possible Duplicate:
How use $.ajax() method in struts2

simply,

1.can sombody tell me how to call a struts action through jquery ajax?(not struts jquery plugin)

2.and how can get results and how can send html output(result) to a target div correctly?

3.should we change the result types in struts configurations?

4.how to run javascript after ajax content have loaded?

5.and any other things i should have known about struts actions regarding jquery ajax and javascript running.

please somebody can share a best reference for this or clean answer.

</div>

Although you can find bits and pieces of this information on the net easily. Let's put it at same page.

Ans 1::Include the js file of JQuery and Write your ajax call.

$(document).ready(function() {
$.ajax({
    type: "POST",   //Default is GET
    cache : false, 
    data: sendingData,           //Data you need to send if in JSON format
    dataType: 'json',     //If json is required
    contentType: 'application/json; charset=utf-8',
    url: "MYACTIONNAME.action",          //URL you need to pass
    success: function(value) {
        alert(value.properties);        
    },
    error: function (xhr, ajaxOptions, thrownError) {
        $(".errors").html("Please Try Again");
        //console.log(xhr.status + thrownError);
    }
});

});

That is for sending JSON and retrieving JSON from action class. Configuration of JSON can be found here along with other answers

Ans 2::

$(document).ready(function() {
        $("#div").load("MYActionName.action"); //can do that through $.ajax also
});

If you want to show only part of the loaded HTML(converted JSP) , use

$("#divToReplaceWithNewOne").hide().load('MyActionName.action #newDivWhichWillbeFilled').fadeIn(1000);  //with animation

Ans 3:: It all depends on what you want . You have to configure if you want pdf or other thing.

a) Whole JSP

<action name="MYActionName" class="MYActionNameBean" method ="execute">
            <result name="success" type="dispatcher">
                <param name="location">/jsp/MyNewPage.jsp</param>
            </result>
        </action>

b) Working with streams and setting content through ActionClass only

<action name="MYActionName" class="MYActionNameBean"    method="execute">
            <result type="stream">
                    <param name="contentType">text/html</param>
                    <param name="inputName">inputStream</param>
            </result>
        </action>

Method::

public String execution() throws Exception {
        try{
            PrintWriter outWriter = null;
            StringBuffer sbf = new StringBuffer("");
            HttpServletResponse httpResponse = ServletActionContext.getResponse();
            try {
                outWriter = httpResponse.getWriter();
                                        sbf.append("String to be sent to View");
                    }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{

                if(outWriter!=null){
                    httpResponse.setContentType("text/html");
                    outWriter.println(sbf.toString());
                    outWriter.flush();
                    outWriter.close();
                }
            }

        }catch (Exception e) {
            throw new MyOwnException(e);
        }
        return null;
        }

c) With JSON as result. (struts2-json plugin)

<action name="MYActionName" class="MYActionNameBean"    method="execute">
            <result type="json"></result>   
        </action>

d) PDF

<action name="DownloadPdf" class="PrintPdfActionBean" method="executeViewPdf">
            <result name="success" type="stream">
                <param name="contentType">application/pdf</param>
                <param name="inputName">fileInputStream</param>
                <param name="contentDisposition">attachment; filename="${fileName}.pdf"</param>  //filename is variable in action class
                <param name="bufferSize">4096</param>
            </result>
        </action>

Ans::4. Thats all JQuery In success/done/deferred callbacks you can put your js Example snippet::

success: function(value) {
            var respStat = data.status; 
                var errorStat = data.errorsMessages;
                if(respStat =="success"){
                    responseMsg.removeClass().addClass('successMessage').html("Your changes have been saved successfully.").fadeOut(4000);
                    }
                else{
                    responseMsg.removeClass().addClass('errorMessage').html(errorStat[0]).show();
                }
},