About Struts-json to improve development efficiency first, what is JSON? : JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy to read and write, but also easy to machine solution. Analysis and generation. It is based on a subset of JavaScript (standard ECMA-262 3rd edition-december 1999). JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. Ii. What support does struts2 have for JSON? : Struts2 provides a struts2-json-plugin.jar to support the use of JSON. When we use JSON in Struts2, Must import Struts2-json-plugin.jar And I need a few points: 1.<package name= "struts2" extends= "Json-default" > Must be inherited from the definition in the Struts-plugin.xml file in Struts2-json-plugin.jar Json-default Struts-plugin.xml configuration information in/*struts2-json-plugin.jar: <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"/> &L T;/interceptors> </package> </struts> By configuring the information we can know: 1.json-default actually inherited the Struts-default. 2. Defines a return type named JSON and an interceptor with the name JSON */ 2.<action name= "*" class= "*" ><result name= "Success" type= "JSON"/></action>//we need to set the result The return type is defined as JSON third, the function of some properties of JSON in Struts.xml. Java code
- <action name= "*" class= "*" >
- <result name= "Success" type= "JSON" >
- <param name= "" ></param>//Some definitions of JSON
- /*excludeproperties//represents excluding which properties in the action, when excluding multiple attributes, separated by commas (that is, properties that do not require serialization)
- Example: <param name= "excludeproperties" >age</param> exclude Getage () How this method is excluded this method is explained below
- Includeproperties//represents which properties are included in the action, including multiple attributes, separated by commas (that is, properties that need serialization)
- Example: <param name= "Includeproperties" >address</param> contains getaddress () This method is included in the following explanation
- Excludenullproperties//Represents a field in which an empty property value is excluded from action
- Example: <param name= "excludenullproperties" >true</param> using true/false default to False (that is, a field containing a null property value)
- Root//represents where to start serialization
- Example: <param name= "root" >person</param> person can be a property or an object or a collection
- When defined here, only the person is serialized, and if the person is an object, all getter methods in the person object are serialized
- Note: If the same attribute defines both Excludeproperties and includeproperties
- Then the priority of the excludeproperties is high, so it is not
- Serialization of this property
- */
- </result>
- </action>
Iv. about the use of JSON in the Action object annotation. : For JSON annotations There are a few properties that you can defineName: Specify the names for serialization/* Example: @JSON (name = "MyName") the username attribute corresponds to the JSON received by the client: {myname: "Zhang San"} was previously {username: "Zhang San"}Public String GetUserName () {return username; }*/Serialize: Serialization of this method True/false/*Example: @JSON (serialize=true) serialization of the method, or False if it is not serializedPublic String GetUserName () {return username;}*/Deserialize: Do not serialize the method True/false/*Example: @JSON (deserialize=true) does not serialize the method, and if False it is serializedPublic String GetUserName () {return username;}*/format: Sets the way in which JSON converts this method, which is typically used to convert time/*Example: @JSON (format= "YYYY-MM-DD") the value of the date corresponding to the JSON received by the client is:{Date: "2011-10-2"}Public Date getDate () {return date;}*/v. Specific examples:Java code
- Action:
- Package org.viancent.action;
- Import Java.util.Date;
- Import Org.apache.struts2.json.annotations.JSON;
- Import Com.opensymphony.xwork2.ActionSupport;
- public class Getjsonaction extends Actionsupport {
- Private String username;
- private String password;
- private int age;
- private date date;
- @JSON (serialize = False)
- Public String GetUserName () {
- return username;
- }
- public void Setusername (String username) {
- This.username = Username;
- }
- @JSON (name= "MyPassword")
- Public String GetPassword () {
- return password;
- }
- public void SetPassword (String password) {
- This.password = password;
- }
- public int getage () {
- return age;
- }
- public void Setage (int.) {
- This.age = age;
- }
- @JSON (format= "YYYY-MM-DD")
- Public Date getDate () {
- return date;
- }
- public void SetDate (date date) {
- This.date = date;
- }
- @Override
- Public String Execute () throws Exception {
- This.setdate (New Date ());
- This.setage (10);
- This.setpassword ("World");
- This.setusername ("Hello");
- return SUCCESS;
- }
- }
Struts.xml Java code
- <struts>
- <package name= "struts2" extends= "Json-default" >
- We must inherit json-default here.
- <action name= "getjsons" class= "Org.viancent.action.GetJsonAction" >
- <result name= "Success" type= "JSON" >
- Be sure to define the Type property as JSON
- <param name= "Excludeproperties" >age</param>
- Exclude Getage () method
- </result>
- </action>
- </package>
- </struts>
Jsp: Java code
- <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
- <script type= "Text/javascript" src= "web-js/jquery-1.5.1.js" ></script>//using jquery Be sure to import JS
- <script type= "Text/javascript" >//Using jquery for asynchronous commits
- function Getjson () {
- $.post (
- "/struts2/getjsons.action", {},function (returneddata,status)
- {
- if ("Success" ==status)
- {
- alert (returneddata);
- }
- }
- )
- }
- </script>
- <body id= "Thebody" >
- <input type= "button" value= "Getjson" onclick= "Getjson ();" >
- </body>
The results are: {"date": "2011-03-21", "MyPassword": "World"} //Use the browser's bug tool to view alert (returneddata); the only bounce is [Object,object]//Here we can see that the name of the password has been changed to MyPassword and set the Data property, the name is not changed |