JSON Jquery到Struts2的动作

我想将JSON对象从Javscript发送到Struts2 Action。

示例JSON对象

{ "lists":["list1","list2","list3","list4","list5"], "maps": { "key4":"value4","key3":"value3","key5":"value5","key2":"value2","key1":"value1" }, "number1":123456789, "numberarray1":[1,2,3,4,5,6,7,8,9], "string1":"A", "stringarray1":["A1","B1"] } 

我的Jquery Ajax

 $.ajax({ type: 'POST', url: 'json/JSON.action', data: JSON.stringify(data), dataType: 'json', async: false , contentType: 'application/json; charset=utf-8', success: function(){window.alert('Done');} }); 

Struts.xml配置

    

我的动作类

 public class JsonAction extends ActionSupport { private String data; public String getJSON() { return ActionSupport.SUCCESS; } public String getData() { return data; } public void setData(String data) { this.data = data; } } 

我的问题是如何在Action Class中接收JSON对象。

注意:POST OF JSON对象成功..我只是不知道如何通过Action Class接收它。请帮助谢谢

  1. struts.xml条目中有一个拼写错误
  2. 你有没有在struts.xml定义tile结果和拦截器。 请看这个链接
  3. 您要发送到服务器的json不包含任何data密钥。 所以它总是空的。 由于json被表示为对象。 您需要以这种方式将JSON转换为Java对象。

方法1。

lists,maps,number1,numberarray1,string1等创建setter。 在此链接的顶部,定义了执行此操作的方法。 然后,您可以通过这种方式访问​​所有变量。

方法2.在你的javascript中定义一个新对象。

  var sentData ={}; sentData ["sentData "] = data; // And in your ajax call , data: JSON.stringify(sentData), 

在您的操作类中,为此创建getter和setter。

 Map sentData = new HashMap(); 

这将为您提供整个json对象作为Map。