Struts2 asynchronous Interactive data __stream with Ajax

Source: Internet
Author: User
Struts2 using Ajax

Similar to file downloads, the result type needed to be configured in Struts.xml is stream.

<package name= "Default" namespace= "/" extends= "Struts-default" >
    <action name= "XXX" class= "Xxxaction" Method= ' Do () ' >
    <result type= ' stream ' name= ' success ' > <param name= ' contentType ' >text/html
        </param>
        <param name= "InputName" >inputStream</param>
    </result>
</package >

Note Two properties contenttype and InputName,
Where InputName corresponds to the value inputstream the property in action.
The other configurations are generic configurations, and there is no difference.
Let's look at the configuration in action

public class Xxxaction extends actionsupport{

    private InputStream inputstream;
    You need to provide the Set method public
    InputStream getInputStream () {return
        inputstream;
    }
    The public String does () {
        try {
            //Here fills in the business code
            ....
            InputStream = new Bytearrayinputstream ("1". GetBytes ("UTF-8"));
        catch (Exception e) {
            e.printstacktrace ();
        }
        Return "Success";
    }

The Ajax method of jquery can be invoked asynchronously in the JSP to invoke the action method

$ (function () {
    //button click event
    $ (". Btn"). Click (function () {
        var url = this.href;
        var args = {"Time": New Date ()};
        jquery Ajax method
        $.post (URL, args, function (data) {
            //This is a callback function that handles the data returned in the action in the function//that is, the
            InputStream corresponding value
        });     

Basic use is this, if it involves passing objects or collections, it is recommended to use JSON combined with Ajax. The
background action encapsulates data as a JSON object pass. You can use a Third-party plug-in or a struts-led
Json-plugin.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.