我的博客我做主

          我的未來不是夢!
          posts - 9, comments - 10, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
          更多博客請查看:http://www.v5cn.cn
          在使用Struts2的JSON插件,實現Action中的屬性序列化成JSON對象時默認JSON插件會把所有Action中包含getter方法的屬性都序列化到JSON對象中。但是有時候我們并不需要太多的屬性,或者只需要一個屬性。那么怎樣控制屬性序列化到JSON對象中哪?Struts2的JSON插件為我們提供了兩種方式,第一:使用注解的方式控制,第二:使用Struts2的struts.xml配置文件的方式。

          這一講我們主要介紹注解方式。如果大家還不會Struts2+JSON+JQuery的交互方式請查看 http://zyw090111.javaeye.comStruts2+jQuery+JSON實現異步交互的文章

          我們要使用JSON的注解是@JSON這個類共有是個屬性分別是:
          1. name    String 類型     用戶為屬性起一個別名(我們序列化到JSON對象中的鍵默認是屬性名稱,如果使用了name屬性那么鍵是name起的名字);
          2. serialize  Boolean類型   默認為true 也就是可以被序列化,如果設為false那么該屬性將不包含在JSON對象中;
          3. format  String類型  主要是對日期進行格式化
          4. deserialize Boolean類型 默認為true,它是指反序列化,和serialize相反。
          請看代碼:
          package test.json;

          import java.util.Date;

          import org.apache.struts2.json.annotations.JSON;

          import com.opensymphony.xwork2.ActionSupport;

          @SuppressWarnings(
          "serial")
          public class Users extends ActionSupport {
              
          private int id;
              
          private String userName;
              
          private String pwd;
              
          private String address;
              
          private Date birthday;
              
          public int getId() {
                  
          return id;
              }

              
          public void setId(int id) {
                  
          this.id = id;
              }

              @JSON(serialize
          =false)
              
          public String getUserName() {
                  
          return userName;
              }

              
              
          public void setUserName(String userName) {
                  
          this.userName = userName;
              }

              @JSON(name
          ="mm")
              
          public String getPwd() {
                  
          return pwd;
              }

              
          public void setPwd(String pwd) {
                  
          this.pwd = pwd;
              }

              
          public String getAddress() {
                  
          return address;
              }

              
          public void setAddress(String address) {
                  
          this.address = address;
              }

              @JSON(format
          ="yy-MM-dd")
              
          public Date getBirthday() {
                  
          return birthday;
              }

              
          public void setBirthday(Date birthday) {
                  
          this.birthday = birthday;
              }

              @Override
              
          public String execute() throws Exception {
                  
                  
          this.id = 10000;
                  
          this.userName = "zhangsan";
                  
          this.pwd = "00000";
                  
          this.address = "xian";
                  
          this.birthday = new Date();
                  
                  
          return SUCCESS;
              }

          }

          評論

          # re: Struts2+JSON+jQuery實現異步交互數據時選擇要序列化的屬性(一注解方式)  回復  更多評論   

          2012-10-04 14:12 by t
          jtjtyktkt

          # re: Struts2+JSON+jQuery實現異步交互數據時選擇要序列化的屬性(一注解方式)  回復  更多評論   

          2012-11-06 14:27 by 阿斯頓的的士速遞
          阿薩颯颯颯颯

          # re: Struts2+JSON+jQuery實現異步交互數據時選擇要序列化的屬性(一注解方式)  回復  更多評論   

          2014-02-25 11:09 by
          輔導費
          主站蜘蛛池模板: 静宁县| 加查县| 萨迦县| 腾冲县| 大邑县| 绥中县| 西昌市| 五家渠市| 萨迦县| 宝鸡市| 鸡泽县| 牡丹江市| 尤溪县| 西吉县| 安多县| 阜新| 青河县| 日土县| 平利县| 都匀市| 茂名市| 靖安县| 和林格尔县| 仙居县| 阳高县| 内江市| 金坛市| 施秉县| 延津县| 望都县| 新津县| 黄大仙区| 鄢陵县| 白银市| 五寨县| 河津市| 梅州市| 古蔺县| 防城港市| 本溪| 元阳县|