简体   繁体   English

如何使用jQuery将JSON数据发布到Struts2 Action类

[英]How to use jQuery to post JSON data to a Struts2 Action class

I've a problem sending data from jQuery to struts2 action class. 我在将数据从jQuery发送到struts2动作类时遇到了问题。 I have seen the question: JSON Jquery to Struts2 action but I don't understand the solution quite well. 我已经看到了问题: JSON Jquery对Struts2的操作,但我不太了解解决方案。

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. 我想将json数据发送到struts2动作类。 The jQuery code is like this: 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. 但是,在Chrome的开发工具中,我已经看到了提交给服务器端的数据。 But on the server side, I don't know how receive the json data. 但在服务器端,我不知道如何接收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. 我还尝试使用List来接收JSON数据。 Changing "data" type from String to List<Node> , it failed again. 将“data”类型从String更改为List<Node> ,它再次失败。 Probably because I don't quite understand the OGNL model which Struts2 is using. 可能是因为我不太了解Struts2正在使用的OGNL模型。

Please Help Me. 请帮我。 Thank you very much! 非常感谢你!

{"id":"1","code":"111","name":"ddd"}

Step 1 : Create a bean/pojo to accumulate/encapsulate the above fields 步骤1:创建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请求体中进行必要的更改以匹配action-params / fields

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. 以上代码未经测试。

Hey The problem is you are directly posting array of objects. 嘿问题是你是直接发布对象数组。 So Struts2 don't know whicch method to call. 所以Struts2不知道要调用的whicch方法。 Change your json data like below. 像下面这样更改你的json数据。 Then it will work. 然后它会工作。

{"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里面用object读取

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是一个java类包含id,code,name,因为它是setters和getters的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM