简体   繁体   中英

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. I have seen the question: JSON Jquery to Struts2 action but I don't understand the solution quite well.

Here is my problem:

The json array is like this:

[{"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:

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.

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.

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.

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

class MyBean{
    String id,code,name;
    //getters & setters
}

Step 2 : Change your action code to receive a List of 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)

    <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

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. Change your json data like below. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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