[Go] About Struts-json configuration (detailed JSON data support with instance struts2)

Source: Internet
Author: User

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
  1. <action name= "*" class= "*" >
  2. <result name= "Success" type= "JSON" >
  3. <param name= "" ></param>//Some definitions of JSON
  4. /*excludeproperties//represents excluding which properties in the action, when excluding multiple attributes, separated by commas (that is, properties that do not require serialization)
  5. Example: <param name= "excludeproperties" >age</param> exclude Getage () How this method is excluded this method is explained below
  6. Includeproperties//represents which properties are included in the action, including multiple attributes, separated by commas (that is, properties that need serialization)
  7. Example: <param name= "Includeproperties" >address</param> contains getaddress () This method is included in the following explanation
  8. Excludenullproperties//Represents a field in which an empty property value is excluded from action
  9. Example: <param name= "excludenullproperties" >true</param> using true/false default to False (that is, a field containing a null property value)
  10. Root//represents where to start serialization
  11. Example: <param name= "root" >person</param> person can be a property or an object or a collection
  12. When defined here, only the person is serialized, and if the person is an object, all getter methods in the person object are serialized
  13. Note: If the same attribute defines both Excludeproperties and includeproperties
  14. Then the priority of the excludeproperties is high, so it is not
  15. Serialization of this property
  16. */
  17. </result>
  18. </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
  1. Action:
  2. Package org.viancent.action;
  3. Import Java.util.Date;
  4. Import Org.apache.struts2.json.annotations.JSON;
  5. Import Com.opensymphony.xwork2.ActionSupport;
  6. public class Getjsonaction extends Actionsupport {
  7. Private String username;
  8. private String password;
  9. private int age;
  10. private date date;
  11. @JSON (serialize = False)
  12. Public String GetUserName () {
  13. return username;
  14. }
  15. public void Setusername (String username) {
  16. This.username = Username;
  17. }
  18. @JSON (name= "MyPassword")
  19. Public String GetPassword () {
  20. return password;
  21. }
  22. public void SetPassword (String password) {
  23. This.password = password;
  24. }
  25. public int getage () {
  26. return age;
  27. }
  28. public void Setage (int.) {
  29. This.age = age;
  30. }
  31. @JSON (format= "YYYY-MM-DD")
  32. Public Date getDate () {
  33. return date;
  34. }
  35. public void SetDate (date date) {
  36. This.date = date;
  37. }
  38. @Override
  39. Public String Execute () throws Exception {
  40. This.setdate (New Date ());
  41. This.setage (10);
  42. This.setpassword ("World");
  43. This.setusername ("Hello");
  44. return SUCCESS;
  45. }
  46. }

Struts.xml

Java code
  1. <struts>
  2. <package name= "struts2" extends= "Json-default" >
  3. We must inherit json-default here.
  4. <action name= "getjsons" class= "Org.viancent.action.GetJsonAction" >
  5. <result name= "Success" type= "JSON" >
  6. Be sure to define the Type property as JSON
  7. <param name= "Excludeproperties" >age</param>
  8. Exclude Getage () method
  9. </result>
  10. </action>
  11. </package>
  12. </struts>

Jsp:

Java code
  1. <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
  2. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
  3. <script type= "Text/javascript" src= "web-js/jquery-1.5.1.js" ></script>//using jquery Be sure to import JS
  4. <script type= "Text/javascript" >//Using jquery for asynchronous commits
  5. function Getjson () {
  6. $.post (
  7. "/struts2/getjsons.action", {},function (returneddata,status)
  8. {
  9. if ("Success" ==status)
  10. {
  11. alert (returneddata);
  12. }
  13. }
  14. )
  15. }
  16. </script>
  17. <body id= "Thebody" >
  18. <input type= "button" value= "Getjson" onclick= "Getjson ();" >
  19. </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

[Go] About Struts-json configuration (detailed JSON data support with instance struts2)

Related Article

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.