我的博客我做主

          我的未來不是夢!
          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
          輔導費
          主站蜘蛛池模板: 阿克| 尼勒克县| 望都县| 夏邑县| 祁阳县| 汪清县| 富宁县| 晋中市| 库伦旗| 海口市| 泸定县| 台北县| 新营市| 永济市| 女性| 石泉县| 大关县| 太白县| 墨竹工卡县| 上犹县| 惠来县| 鲁山县| 吉水县| 莎车县| 益阳市| 常山县| 大姚县| 清流县| 游戏| 萝北县| 耿马| 永清县| 清丰县| 和硕县| 九台市| 兰考县| 色达县| 米林县| 吴堡县| 巴青县| 会泽县|