struts2 json传递对象

转自:http://blog.csdn.net/dengsilinming/article/details/7422240

谢谢原作者的分享!

我仅是参考!没有实验一下!

——————————————————————————————————————————————————————————————————————

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

struts.xml

  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

  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

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

  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为一个对象时为什么不能直接写在里面,在此标记一下。嗯,有必要去看下官方的api了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值