1. Introduction of JSON plug-in package
<!--Introducing Struts2 JSON plugins--<dependency> <groupId>org.apache.struts</groupId> <a Rtifactid>struts2-json-plugin</artifactid> <version>${struts.version}</version> </depend Ency>
2. Configure xml,extends= "Json-default", <result name= "Success" type= "JSON" ></result>
<?xml version= "1.0"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1.7//en" "http://struts.apache.org/ Dtds/struts-2.1.7.dtd "><struts> <package name=" indexpackage "namespace="/"extends=" Json-default "> <action name= "indexaction_*" class= "indexaction" method= "{1}" > <result name= "Success" type= "JSON" ;</result> </action> </package></struts>
3. Use in action
package cn.sniper.project.action;import org.apache.struts2.json.annotations.json; @Controller (" Indexaction ") @Scope (" prototype ") public class indexaction extends baseaction<uservo> { private static final long serialversionuid = -8269406543148672672l; //login default is fail private string login = fail; /** * Jump to the login page * @return */ public string login () { return login; } /** * Sign in * @return */ public striNg dologin () { UserVo user = This.getmodel (); if (User.getusername (). Equals (User.getPassword ( ))) { this.setlogin (SUCCESS); // JSON can only get the stack top element super.push (This.login); return success; } else { throw new RuntimeException (); } } /** * after successful login home * @return */ public string index () { return index; } @ JSON (name= "login") public string getlogin () { return login; } public void setlogin (String login) { this.login = login; }}
4. JSON Related:
A, the JSON object passed to the page is stack top element B, rename the JSON object @JSON (name= "login") public String GetLogin () {return login; }c, setting property does not do JSON serialization @JSON (serialize=false) public String GetLogin () {return login; } d, formatted date @JSON (format= "YYYY-MM-DD") public String getDate () {return date; }
5, the JSON plug-in principle, view the Struts2-json-plugin.jar root directory under the Struts-plugin.xml file, found that Json-default itself inherits from Struts-default, and custom type JSON:
<?xml version= "1.0" encoding= "UTF-8" ?><! doctype struts public "-//apache software foundation//dtd struts configuration 2.3//en " " HTTP://STRUTS.APACHE.ORG/DTDS/STRUTS-2.3.DTD " ><struts> <package name= "Json-default" extends= "Struts-default" > <result-types> <result-type name= "JSON" class= " Org.apache.struts2.json.JSONResult "/> </result-types> <interceptors> <interceptor name= "JSON" class= " Org.apache.struts2.json.JSONInterceptor "/> <intercepTor name= "Jsonvalidation" class= "Org.apache.struts2.json.JSONValidationInterceptor"/> <!-- sample json validation stack --> < Interceptor-stack name= "Jsonvalidationworkflowstack" > <interceptor-ref name= "BasicStack"/> <interceptor-ref Name= "Validation" > <param name= "Excludemethods" >input,back,cancel</param> </ interceptor-ref> <interceptor-ref name= " Jsonvalidation "/> <interceptor-ref name= "Workflow"/> </interceptor-stack> </interceptors > </package></struts>
6. Visit the page:
$.ajax ({ url : baseURL + '/indexaction_dologin.action ', type : ' Post ', data : { username : $ (' #username '). Val (), password : $ (' #password '). Val (), }, beforeSend : function () { &nBsp; $.messager.progress ({ text : ' Logging in ... ', }); }, success : function (data, Response, status) { $.messager.progress (' close '); /** * Why does data not use a Boolean type? * because if data is not NULL, it is a true value. This will cause the login to be successful if the login failure returns a non-null value */ if (data== "Success") { location.href = baseURL + '/indexaction_index.action '; } else { $.messager.alert (' Login failed! ', ' username or password is wrong! ', ' warning ', function () { $ ( ' #password '). Select (); }); } } });
STRUTS2 's JSON plugin