发送错误&使用 struts2 json 插件通过 jQuery 的 getJSON 接收数据映射
前言:
我正在使用 Struts2 JSON 插件,并且一切似乎都已正确配置。
我可以从 json 操作接收 JSON 数据,效果很好。
我还可以通过 getJSON 发送简单数据(只是一个字符串,一个仅包含简单字符串键:值对的映射),也不会出现错误。他们按照我的预期为我的操作设置了适当的属性。
但是,当我尝试发送包含数组或另一个地图的地图时,我开始在服务器端收到错误。
更新并进行清理,以更好地展示我正在尝试的内容,希望能减少混乱。
不幸的是,我已经不再写测试内容了,所以事情发生了很大的变化。 有意义的操作片段:
private Map<String, String> viewedEntries;
public Map<String, String> getViewedList() {
return viewedEntries;
}
public void setViewedList(Map<String, String> viewedList) {
this.viewedEntries = viewedList;
}
public String submitViewedPDIJson() {
if (viewedEntries!=null && viewedEntries.size()>0) {
for (String key: viewedEntries.keySet()) {
System.out.println("viewedList[" + key + "] = " + viewedEntries.get(key));
}
} else {
System.out.println("No viewedList values.");
}
return Action.SUCCESS;
}
操作定义:
<action class="com.xxxxx.broker.struts.actions.PdiAction" method="submitViewedPDIJson" name="submitViewedPDIJson">
<result type="json">
<param name="noCache">true</param>
</result>
</action>
Javascript:
$("#submitViewed").submit(function(){
$.getJSON('pdi/submitViewedPDIJson.action', {"viewedEntries": {"broker1": true, "broker6": false}},function(data) {
console.log('submitted viewed entries map');
});
return false;
});
服务器端错误:
15:00:12.505 ["http-bio-8080"-exec-9] WARN c.o.xwork2.ognl.OgnlValueStack - Error setting expression 'viewedEntries[broker1]' with value '[Ljava.lang.String;@cc831a'
ognl.OgnlException: target is null for setProperty(null, "null", [Ljava.lang.String;@cc831a)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2219) ~[xwork-core-2.1.6.jar:na]
at ognl.ASTProperty.setValueBody(ASTProperty.java:127) ~[xwork-core-2.1.6.jar:na]
我可能在这里做了一些根本错误的事情,这是错误的方法。我希望做的是让服务器上的地图由 getJSON 调用发送的数据填充。可能还有一些其他数据。对于简单的东西来说,它似乎工作得很好,但是在地图内混合地图等,则不起作用。 如果这不是正确的方法,我将不胜感激任何正确方向的指示。
Preamble:
I'm using the Struts2 JSON plug-in, and it all appears to be configured properly.
I can receive JSON data from my json action and that works fine.
I can also send simple data via getJSON (just a string, a map with only simple string key:value pairs) also without error. They set the appropriate properties on my Action as I would expect.
However, when I try to send a map that contains either an array, or another map, I start to get errors on the server side.
Updated and cleaned up to show what I'm trying a bit better, with hopefully less clutter.
Unfortunately, I've moved on from the test stuff I was writing so things have changed a fair bit.
Meaningful Slice of Action:
private Map<String, String> viewedEntries;
public Map<String, String> getViewedList() {
return viewedEntries;
}
public void setViewedList(Map<String, String> viewedList) {
this.viewedEntries = viewedList;
}
public String submitViewedPDIJson() {
if (viewedEntries!=null && viewedEntries.size()>0) {
for (String key: viewedEntries.keySet()) {
System.out.println("viewedList[" + key + "] = " + viewedEntries.get(key));
}
} else {
System.out.println("No viewedList values.");
}
return Action.SUCCESS;
}
Action Definition:
<action class="com.xxxxx.broker.struts.actions.PdiAction" method="submitViewedPDIJson" name="submitViewedPDIJson">
<result type="json">
<param name="noCache">true</param>
</result>
</action>
Javascript:
$("#submitViewed").submit(function(){
$.getJSON('pdi/submitViewedPDIJson.action', {"viewedEntries": {"broker1": true, "broker6": false}},function(data) {
console.log('submitted viewed entries map');
});
return false;
});
Server-Side Error:
15:00:12.505 ["http-bio-8080"-exec-9] WARN c.o.xwork2.ognl.OgnlValueStack - Error setting expression 'viewedEntries[broker1]' with value '[Ljava.lang.String;@cc831a'
ognl.OgnlException: target is null for setProperty(null, "null", [Ljava.lang.String;@cc831a)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2219) ~[xwork-core-2.1.6.jar:na]
at ognl.ASTProperty.setValueBody(ASTProperty.java:127) ~[xwork-core-2.1.6.jar:na]
It's possible I'm doing something fundamentally wrong here, and this is the wrong approach. What I'm hoping to do is have my Map on the server populated by the data sent by the getJSON call. And possibly some other data as well. It seems to work fine for simple stuff, but mixing in maps-within-maps, etc, doesn't work.
If this is not the correct approach, I would appreciate any pointers in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论