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.