在struts2中,接受ajax传的json对象集合,接收不到

来源:互联网 发布:淘宝怎样代理商品 编辑:程序博客网 时间:2024/04/29 05:49

转自百度知道:http://zhidao.baidu.com/question/480218713.html


实例如下:action中,action中List<JsonTest> jsonTest = new ArrayList<JsonTest>();有jsonTest的get,set方法;JsonTest有属性t_id,t_name,有set,get方法,js:$.ajax({              type:"POST",              async:false,             url :"<%=basePath%>pages/User/getUserObj.action",                      data : {                'userName' : '薛军军',                'sex' : '男',                'others' : '其他值',                'jsonTest':[{'t_id':'sd','t_name':'trs'},{'t_id':'sd','t_name':'trs'}]               },               dataType : "text",               success : function(data) {                alert(data);               }        });报错:16:09:30,687  WARN OgnlValueStack:60 - Error setting valuejava.lang.NullPointerException    at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:160)    at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)    at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2315)    at ognl.ASTProperty.setValueBody(ASTProperty.java:127)    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)    at ognl.SimpleNode.setValue(SimpleNode.java:301)    at ognl.ASTChain.setValueBody(ASTChain.java:227)    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)    at ognl.SimpleNode.setValue(SimpleNode.java:301)    at ognl.Ognl.setValue(Ognl.java:737)谢谢回答


不知道你解决了没有。我刚好遇到这个问题,基本上和你差不多。你这个实际上是复杂数据传递。因为你的json中又包含了JsonArray,array中又存储了json对象......因为你是采用jquery的Ajax进行传递的,但是jquery的ajax和struts并不是很兼容,传递过去的值,根本无法被被正确的解析。那么如何解决这个问题呢?这样改var myparam =  var myparam = JSON.stringify(                {                'userName' : '薛军军',                'sex' : '男',                'others' : '其他值',                'jsonTest':[{'t_id':'sd','t_name':'trs'},{'t_id':'sd','t_name':'trs'}]               },        );$.ajax({              type:"POST",              async:false,             url :"<%=basePath%>pages/User/getUserObj.action",                      data : myparam,               dataType : "text",               success : function(data) {                alert(data);               }        });这样就向后台传递了正确的json对象。但是现在后台还无法正确接收对象,还需要配置struts.xml,在你的包中,action前面加上拦截器  <interceptors>   <interceptor-stack name="myStack">    <interceptor-ref name="json"></interceptor-ref>    <interceptor-ref name="defaultStack" />   </interceptor-stack>  </interceptors>    <default-interceptor-ref name="myStack" />好了,这样就应该可以了

原创粉丝点击