我的博客我做主

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

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

          我們要使用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實現(xiàn)異步交互數(shù)據(jù)時選擇要序列化的屬性(一注解方式)  回復  更多評論   

          2012-10-04 14:12 by t
          jtjtyktkt

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

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

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

          2014-02-25 11:09 by
          輔導費
          主站蜘蛛池模板: 连山| 澜沧| 广灵县| 普安县| 万盛区| 望奎县| 星子县| 马龙县| 佛学| 德安县| 沂水县| 阳春市| 大厂| 任丘市| 榆社县| 巍山| 乌恰县| 苏尼特左旗| 丽江市| 旺苍县| 西城区| 甘肃省| 汶川县| 长岛县| 青冈县| 广东省| 平和县| 前郭尔| 利津县| 蒙山县| 林周县| 阳曲县| 松溪县| 黄石市| 星座| 公主岭市| 武宁县| 星子县| 清丰县| 湛江市| 上杭县|