After mastering the interaction between Java objects and JSON, today's learning is how to use Ajax to request action and put back JSON characters, parsing JSON data in the foreground.
Step one: Use Ajax post to submit directly, write a post.jsp file
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Then there is a JS (post.js) file corresponding to it, which is used to write the Post submission:
$ (function () {
$ (' #bt '). Click (function () {
var result=$ ("#name"). Val ();
Use the $.post method to submit
$.post (
"postaction",//) specify the URL
{' result ': result}, //pass parameters, use Struts2 Injection parameters in the way (there is a corresponding property in action, setter method)
function success (data) {//callback function
alert (data) after the successful commit;
var r = $.parsejson (data); Packet data resolves to JSON format
alert (r.name);
$ ("#result"). HTML (r.name);
},
"JSON" //Specifies that the returned data is a JSON string
)
;
The corresponding postaction:
public class Postaction extends Actionsupport {
private String result;
Public String GetResult () {return result
;
}
public void Setresult (String result) {
This.result = result;
}
@Override public
String execute () throws Exception {
jsonobject jobject=new jsonobject (); map<string,object> map=new hashmap<string,object> (); map.put ("name", result);
Jobject.element ("name", result);
Result=jobject.tostring ();
return Super.execute ();
}
Struts configuration file:
? xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
<package name=" Login "extends=" Json-default ">
<action Name= "postaction" class= "edu.hue.jk.action.PostAction" >
<result type= "json" >
<param Name = "Root" >result</param> <!--root indicates the hierarchy of the returned object is the root result is the return results-->
</result>
</ action>
</package>
</struts>
results:
Click on the button:
Step two: Use Ajax directly to request action:ajax.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Ajax.js:
$ (function () {
$ (' #bt '). Click (function () {
var name=$ ("#name"). Val ();
$.ajax ({
type: Post),
dataType: ' json ',//Accept data Format
cache:false,
data:{' result ': name},
URL: ' Ajaxaction ",
beforesend:function (XMLHttpRequest) {
},
success:function (data) {
alert (data);
var r = $.parsejson (data); Parses a string into a JSON object
$ ("#result"). HTML (r.name);
},
error:function () {
//Request error Handling
alert ("Error !");
}
});
});
});
Ajaxaction:
$ (function () {
$ (' #bt '). Click (function () {
var name=$ ("#name"). Val ();
$.ajax ({
type: Post),
dataType: ' json ',//Accept data Format
cache:false,
data:{' result ': name},
URL: ' Ajaxaction ",
beforesend:function (XMLHttpRequest) {
},
success:function (data) {
alert (data);
var r = $.parsejson (data); Parses a string into a JSON object
$ ("#result"). HTML (r.name);
},
error:function () {
//Request error Handling
alert ("Error !");
}
});
});
});
Struts configuration file:
<package name= "Login" extends= "Json-default" > <action name= "ajaxaction" class= "
Edu.hue.jk.action.AjaxAction ">
<result type=" json ">
<param name=" root ">result</ Param> <!--root indicates that the hierarchy of the returned object is the root result is the return results-->
</result>
</action>
</ Package>
Results: