struts2 json传递对象

来源:互联网 发布:折800官网9.9包邮淘宝 编辑:程序博客网 时间:2024/06/05 06:28

今天在用struts2 异步请求从后台传一个对象到前台时遇到了一个小小的问题,现在此作一个标记,把主要的代码贴上以作备忘。

struts.xml

[html] view plaincopy
  1. <action name="getSystemContactorInfo" class="userAction" method="getSystemContactorInfo">  
  2.             <result type="json">  
  3.                 <param name="includeProperties">systemContactor\.userName,systemContactor\.cellPhone,  
  4.                     systemContactor\.notesMail  
  5.                 </param>  
  6.                 <!-- <param name="includeProperties">systemContactor</param> 这样不对-->  
  7.                 <!-- <param name="includeProperties">systemContactor.*</param> 这样同样不正确-->  
  8.             </result>  
  9.         </action>  

UserAction

[java] view plaincopy
  1. public String getSystemContactorInfo() {  
  2.         UserModel systemContactor = new UserModel();  
  3.         systemContactor.setUserName("张三");  
  4.         systemContactor.setCellPhone("13240151465");  
  5.         systemContactor.setNotesMail("test@126.com");  
  6.         return Action.SUCCESS;  
  7.      }  
UserModel

[java] view plaincopy
  1. public class UserModel{  
  2.    private String userName;  
  3.    private String cellPhone;  
  4.    private String notesMail;  
  5. //省略get、set方法  
  6. }  
jsp页面中

[html] view plaincopy
  1. $.ajax({  
  2.             type:'post',  
  3.             url: 'getSystemContactorInfo.action',  
  4.             dataType: 'json',  
  5.             async: true,  
  6.             success: function showContent(json) {  
  7.                 var userInfo = json.systemContactor;  
  8.                 $("#userName").html(userInfo.userName);  
  9.                 $("#cellPhone").html(userInfo.cellPhone);  
  10.                 $("#notesMail").html(userInfo.notesMail);  
  11.             }  
  12.         });  
<param name="includeProperties">systemContactor</param> 当systemContactor为一个字串或数字时可以这样写
<param name="includeProperties">systemContactor.*</param>当systemContactor为list时可这样传
到现在也不是太明白当systemContactor为一个对象时为什么不能直接写在里面
0 0
原创粉丝点击