Struts2+hibernate+spring+jquery back to the JSON list list

Source: Internet
Author: User
Tags string back what interface

1. Introduction Package: Struts2-json-plugin-2.1.8.1.jar Json-lib-2.1.jar Commons-collections-3.2.1.jar Commons-beanutils-1.8.2.jar Commons-lang-2.4.jar
Ezmorph-1.0.6.jar, the other baullosa, these packages are required to return data in JSON form
2.<package name= "Default" extends= "Json-default" >
3.<action name= "test" class= "com. Testaction "method=" Test >
<interceptor-ref name= "JSON"/><!--processing requests submitted in JSON text--
<result type= "JSON"/><!--returns the Bean property of the action as a JSON string to the browser--
</action>
4. As long as the extends= "Json-default" is inherited, the JSON interceptor is the default configuration and can not be used. After result is set to JSON, the container automatically encapsulates the action's properties into a JSON object (the JSON interceptor does) and then calls the JS callback method. Return JSON data
5. If you follow the configuration in 3. You'll find that the JSON string returned by the foreground is the return of all the attributes in the action to the JSON string back to the browser (and sometimes no results, no error, background execution, but the foreground does not callbackfunction), But we sometimes need to return some of the results according to the actual situation, how to customize the output of the JSON results? Result provides some parameters to solve this problem for you, the most commonly used is the includeproperties parameter and the excludenullproperties parameter. Of course there are other methods, such as adding JSON annotations to the attributes of Pojo.
6.includeProperties parameter: The attribute value to be included in the output, where the regular expression and the property name match, you can use "," to split the fill of multiple regular expressions. This parameter returns the JSON data of the object directly, the foreground does not need the Eval conversion, the <paramname= "root" >result</param> is different, requires the foreground to perform the eval conversion
For example: Output all properties of person
<result type= "JSON" >
<param name= "Includeproperties" >person.*,person\.name</param>
</result>
7.excludeProperties parameter: The output result needs to reject the attribute value, also supports the regular expression matches the attribute name, may use "," the partition fills many regular expressions, similar includeproperties
8. Output a JSON list
<action name= "list" class= "testaction" method= "List" >
<result name= "Success" type= "JSON" >
<param name= "Includeproperties" >
List\[\d+\]\. Id,list\[\d+\]\.user\.username
</param>
</result>
</action>
Where list is a property of the list type in action
List\[\d+\]\. The ID represents the object stored in list 0: The ID property of end (the object stored in list must have an id attribute).
List\[\d+\]\.user\.username represents the Username property of the user object in the list object
9. Why use Includeproperties or excludeproperties parameters:
Mainly in order to filter out the interface, Pojo set, list, other objects and other unwanted data to prevent the loop to take other objects or not found. If not configured, the default is to handle all the properties in the action, and if there is an interface injection in the action, the JSON interceptor may not be able to find the result, and if there is an object in the action, the object is associated with many objects. The JSON interceptor converts the properties of all associated objects into JSON format, if other objects have a list, set, and return the result ... There could be a dead loop, unable to return
10. Summary:
Avoid the action method that starts with get in action, removing the Get method for the interface in the action
Configures parameters such as includeproperties,excludeproperties for JSON type result.

ALSO: http://cqjava.iteye.com/blog/465495

http://ld-hust.iteye.com/blog/626571

To facilitate the transfer of data to Ajax calls, the JSON plug-in added in Struts2 is used to serialize and deserialize objects, and the JSON plugin's

http://code.google.com/p/jsonplugin/

1. Download the JSON plug-in package and copy the jar package to the Web-inf/lib directory

Note: struts2 JSON plug-in has two versions 0.32 and 0.34, I use the struts2 version is 2.0.11, when added to the 0.34 version of the JSON plug-in when the exception started, prompted to find the relevant method, and then replaced by a low version of 0.32 all OK, carefully review the document found STRUTS2 2.0.* version of the corresponding plug-in version 0.32, struts2.1.* version corresponding to 0.34 version

2.json plug-in execution principle timing diagram

Click to view the original

3. Put the struts.xml inside

Xml Code   

    1. <package name= "Default" extends= "Struts-default">

Switch

Xml Code   

    1. <package name= "Default" extends= "Json-default">

Configuration in 4.Action

Xml Code   

    1. <action name= "testaction" class= "com.json.action.TestAction" method= "TestMethod">
    2. <interceptor-ref name= "json" /><!--processing requests submitted in JSON text--
    3. <result type= "json" /> <!--returns the Bean property of the action as a JSON string to the browser--
    4. </action>

Note: Select interceptor and result configurations as needed

5. You will find that the JSON string returned by the foreground is the conversion of all properties in the action to a JSON string returned to the browser, but I sometimes need to return some of the results according to the actual situation, how to customize the results of the JSON output? Result provides some parameters to solve this problem for you.

5.1. Root parameter: Take out the results you need to output from the returned results according to the OGNL expression

Such as:

Action class

Java Code   

    1. Public class Baseaction extends actionsupport implements {
    2. Private person person = null;
    3. ...
    4. }

Bean class

Java Code   

    1. Public class person {
    2. Private String name;
    3. Private int age;
    4. ...
    5. }

We just output the Name property value of the person object, which is configured as follows

Xml Code   

    1. <result type= "JSON">
    2. <param name= "root">person.name</param>
    3. </result>

5.2. Excludenullproperties parameter: Indicates whether null values are removed, the default value is False, and if set to true automatically filters for empty values, only outputs values that are not empty.

Xml Code   

    1. <result type= "JSON">
    2. <param name= "excludenullproperties">true</param>
    3. </result>

5.3. Ignorehierarchy parameter: Indicates whether the hierarchy is ignored, that is, the inheritance relationship, such as: Testaction inherits from Baseaction, The JSON string returned in Testaction, by default, does not contain the property value of the parent class baseaction, the Ignorehierarchy value defaults to True, and the property of the parent class and the subclass is returned when set to False.

Xml Code   

    1. <result type= "JSON">
    2. <param name= "Ignorehierarchy">false</param>
    3. </result>

5.4. Includeproperties parameter: The value of the attribute that needs to be included in the output, where the regular expression matches the property name, and multiple regular expressions can be populated with "," splits.

For example: Output all properties of person

Xml Code   

    1. <result type= "JSON">
    2. <param name= "includeproperties">person.*, person\.name</param>
    3. </result>

5.5. Excludeproperties parameter: The output results need to reject the attribute values, also support the regular expression matching the property name, you can use "," split fill multiple regular expressions, similar to 5.4.

JSON (Java Script Object Notation), which is a language-independent data Interchange format.
The JSON plugin is a STRUCTS2 Ajax plugin that enables developers to easily and flexibly exploit Ajax through the use of JSON plugins.
JSON is a lightweight data interchange format, and the JSON plugin provides a actionresulttype called JSON.
Once the result processing type is specified for the action, the JSON plug-in automatically serializes the data in the action into JSON-formatted data.
and returns the JavaScript to the client's physical view. Simply put, the JSON plugin allows us to invoke the action asynchronously in JavaScript,
And action does not need to specify a view to display information about the action.
Instead, the JSON plugin is responsible for specifically returning the specific information in the action to the calling page.
The JSON data format can be as simple as: Person = {name: ' Jim ', Age:18,gender: ' Man '}.
If the action has many properties, we want to return the data from the action to the calling page.
Configure Includeproperties or excludeproperties interceptors at this time.
The 2 interceptors are defined in the Struts2 Json-default package, so the packets to use the interceptor are inherited from Json-default.
<struts>
<constantname= "Struts.objectfactory" value= "Spring"/>
<includefile= "Struts-admin.xml" ></include>
<packagename= "Default" extends= "Json-default" >
<action name= "Person" class= "com.person.PersonAction" method= "View" >
<result type= "JSON" >
<paramname= "Includeproperties" >
Person\.name,persoon\.age,person\.gender
</param>>
</result>
</action>
</package>
</struts>
With the supported configurable results of struts 2, the effect of the filter can be achieved. The action's processing result configuration supports regular expressions.
However, if the returned object is an array-formatted JSON data. For example, there are object Persion1...person9 in Pesonbean, and I just person1 json data,
You can use the following regular expression.
<struts>
<constantname= "Struts.objectfactory" value= "Spring"/>
<includefile= "Struts-admin.xml" ></include>
<packagename= "Default" extends= "Json-default" >
<action name= "Person" class= "com.person.PersonAction" method= "View" >
<result type= "JSON" >
<paramname= "Includeproperties" >
Person\[\d+\]\.person1
</param>>
</result>
</action>
</package>
</struts>
The use of the Excludeproperties interceptor is similar to this, if only one object is intercepted, if the entire object of the person bean is intercepted.
<struts>
<constantname= "Struts.objectfactory" value= "Spring"/>
<includefile= "Struts-admin.xml" ></include>
<packagename= "Default" extends= "Json-default" >
<action name= "Person" class= "com.person.PersonAction" method= "View" >
<result type= "JSON" >
<paramname= "Excludeproperties" >
Person
</param>>
</result>
</action>
</package>
</struts>

It is important to note that if you use a JSON plugin to set the return result as JSON. The principle of JSON is that the GET method in the action will be serialized,
So the previous method of get gets executed as long as no serialization is specified.
If the method must be named get* (for example, what interface is implemented),
You can then add annotations to the method before declaring that the method does not serialize.
Annotations are as follows: @JSON (Serialize=false)

In addition to this, JSON annotations support several fields:
Serialize: Sets whether the property is serialized
Deserialize: Sets whether the property is deserialized.
Format: Sets the formatting used to format the output and resolve the Date form fields. For example "Yyyy-mm-dd ' T ' HH:mm:ss".

Use the comment syntax to change the property name after the property is serialized
@JSON (name= "NewName")
Public String GetName ()
{
return this.name;
}
need to introduce Importcom.googlecode.jsonplugin.annotations.JSON;

@JSON (Serialize=false)
Public User GetUser () {
return this. User;
}

@JSON (format= "YYYY-MM-DD")
Public Date getstartdate () {
return this.startdate;
}

Struts2+hibernate+spring+jquery back to the JSON list list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.