Strut2 provides a json-supported plug-in that adds a struts2-json-plugin-2.1.8.jar to the project.
To write a package in struts. xml, json-default and json-default must be inherited from struts-default.
Next simple example:
<Package name = "inherit" namespace = "/clusters" extends = "json-default"> <action name = "validateprohibited" class = "com. tctoss. action. validateFeeAction "> <result name =" success "type =" json "> <! -- If the root is specified, the specified root is used as the target and a json string. If this parameter is not specified, the default Action is root. --> <Param name = "root"> pass </param> </result> </action> </package>
<action name="json" class="com.tctoss.action.LAction" > <result type="json" name="findData"> <param name="root">datas</param> <param name="includeProperties">success,totalcount</param> </result> <result type="json" name="findTree"> <param name="root">result</param> </result></action>
1. After the type in the result is set to json, the container will automatically encapsulate the action attribute in a json object as the json interceptor), and then call the ajax callback method to return json data.
2. Add a "root" identifier to the data of the root object in JSON. To use it, Action must follow the following rules:
1) The "content-type" must be "application/json" in the returned page type ";
2) The JSON content must comply with the format requirements;
3) The field in the Action must have a public set method (if there is no set method, the field will not be added to the JSON data, to be verified );
4) it supports the following types: Basic Types (int, long... String), Date, List, Map, Primitive Arrays, other classes, and object Arrays;
5) Any Object in JSON will be encapsulated in list or map, and the data will be encapsulated in Long. If it contains data, it will be encapsulated in Double, the array will be encapsulated by List.
2. includeProperties parameter: attribute value to be included in the output result. Here, the regular expression matches the attribute name. You can use "," to separate multiple regular expressions. For example:
<Paramname = "includeProperties"> module. *, user \. userName </param>
Indicates all attributes of the module and the user name.
3. excludeProperties parameter: attribute values to be excluded from the output results. You can also use regular expressions to match attribute names. You can use commas (,) to separate multiple regular expressions, similar to includeProperties.
4. Why use the includeProperties or excludeProperties parameter:
It is mainly used to filter out the interface. The set, list, and other objects of pojo do not need any data to prevent other associated objects from being retrieved cyclically or the interface cannot be found;
If this parameter is not set, all attributes in the action are processed by default. If the action contains interface injection, the json interceptor may fail to find the interface and return the result.If an object exists in an action and is associated with many objects, the json interceptor converts all the attributes of the associated objects to json format.If other objects include list and set, the returned results are huge. It may be an endless loop and cannot be returned.
If you do not use <param name = "includeProperties"> or other methods to filter json data, through debug, you will find that the json string returned by the foreground converts all the attributes in the action into json strings and returns them to the client, including the service interface, all attributes of pojo, and associated pojo. Sometimes the result is not returned at all, and no error is reported. The backend executes the call back function, but the frontend cannot execute the call back function. This is mainly because the interface cannot be found or too many associated pojo, leading to an endless loop ),
The root, includeProperties, and excludeNullProperties parameters are commonly used.
Of course, there are other methods, such as adding json annotations to the attributes of pojo.
5. The name attribute in the result tag indicates the name returned by the action class in struts2, for example:
public String findUserById() throws Exception{ int id=Integer.parseInt( this.request.getParameter("id")); List<User> list=service.findById(id); return "findTree";}
The above code return "findTree" will find the findTree result set, encapsulate it, and return json to the client.
This article from the "beautiful life needs to carefully record" blog, please be sure to keep this http://huing.blog.51cto.com/1669631/1286327