我的博客我做主

          我的未來不是夢!
          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
          輔導費
          主站蜘蛛池模板: 建水县| 沁源县| 大同县| 庆安县| 洪泽县| 石柱| 西宁市| 余姚市| 长岛县| 五华县| 于田县| 佳木斯市| 嘉兴市| 七台河市| 闵行区| 涞水县| 曲阳县| 泾阳县| 隆昌县| 武平县| 阿合奇县| 岐山县| 治县。| 密云县| 南川市| 建平县| 邛崃市| 罗山县| 台南县| 宜都市| 河北省| 全椒县| 都匀市| 大庆市| 富宁县| 斗六市| 鄂托克前旗| 武夷山市| 雅江县| 拜城县| 黎平县|