Java 如何使用 jQuery 将 JSON 数据发布到 Struts2 Action 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19566700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use jQuery to post JSON data to a Struts2 Action class
提问by Steve Lu
I've a problem sending data from jQuery to struts2 action class. I have seen the question: JSON Jquery to Struts2 actionbut I don't understand the solution quite well.
我在将数据从 jQuery 发送到 struts2 操作类时遇到问题。我见过这个问题:JSON Jquery to Struts2 action但我不太了解解决方案。
Here is my problem:
这是我的问题:
The json array is like this:
json数组是这样的:
[{"id":"1","code":"111","name":"ddd"},
{"id":"2","code":"222","name":"sss"},
{"id":"3","code":"333","name":"eee"}]
I want to send the json data to the struts2 action class. The jQuery code is like this:
我想将 json 数据发送到 struts2 操作类。jQuery 代码是这样的:
var data = JSON.stringify(dataObj);
$.ajax({
url: "Update",
type: "post",
data: data,
dataType: 'json',
contentType:"application/json;charset=utf-8",
success : function(){
alert("You made it!");
}
});
However, in Chrome's Development Tool, I have seen the data submitted to the server side. But on the server side, I don't know how receive the json data.
但是,在Chrome的开发工具中,我看到了提交到服务器端的数据。但是在服务器端,我不知道如何接收json数据。
Action:
行动:
public class Update extends ActionSupport{
private String data;
public String getData(){
return data;
}
public void setData(String data){
this.data= data;
}
public String execute(){
System.out.println(data);
return SUCCESS;
}
}
In this way, data
is null.
这样,data
为空。
I've also tried to use a List to receive JSON data. Changing "data" type from String to List<Node>
, it failed again. Probably because I don't quite understand the OGNL model which Struts2 is using.
我还尝试使用 List 来接收 JSON 数据。将“数据”类型从 String 更改为List<Node>
,它再次失败。可能是因为我不太了解 Struts2 使用的 OGNL 模型。
Please Help Me. Thank you very much!
请帮我。非常感谢!
回答by coding_idiot
{"id":"1","code":"111","name":"ddd"}
Step 1 : Create a bean/pojo to accumulate/encapsulate the above fields
第一步:创建一个bean/pojo来累积/封装上述字段
class MyBean{
String id,code,name;
//getters & setters
}
Step 2 : Change your action code to receive a List of MyBeans
第 2 步:更改您的操作代码以接收 MyBeans 列表
public class Update extends ActionSupport{
private List<MyBean> data;
//other code, getters & setters
}
Step 3: Configure your action to de-serialize JSON data and fill the action fields (using json-plugin)
第 3 步:配置您的操作以反序列化 JSON 数据并填充操作字段(使用 json-plugin)
<action name="Update" class="Update">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
</action>
Step 4 : Make necessary changes in the ajax request body being sent to match the action-params/fields
第 4 步:在发送的 ajax 请求正文中进行必要的更改以匹配操作参数/字段
var data = JSON.stringify(dataObj);
$.ajax({
url: "Update",
type: "post",
data: "data:"+data,
dataType: 'json',
contentType:"application/json;charset=utf-8",
success : function(){
alert("You made it!");
}
});
The above code is untested.
以上代码未经测试。
回答by Rajesh
Hey The problem is you are directly posting array of objects. So Struts2 don't know whicch method to call. Change your json data like below. Then it will work.
嘿问题是你直接发布对象数组。所以Struts2 不知道调用哪个方法。更改您的 json 数据,如下所示。然后它会起作用。
{"data":[{"id":"1","code":"111","name":"ddd"},
"id":"2","code":"222","name":"sss"},
{"id":"3","code":"333","name":"eee"}]}
Then inside the setter read with object
然后在setter里面用对象读取
public void setData(List < Report > data) {
System.out.println("Setter Call Flow");
this.data = data;
}
Where Report is a java class contains id,code,name as it's members with setters and getters.
其中 Report 是一个包含 id、code、name 的 java 类,因为它是具有 setter 和 getter 的成员。