How to use jQuery to post JSON data to a Struts2 Action class

前端 未结 2 1555
臣服心动
臣服心动 2021-02-09 09:58

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.

<
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 09:59

    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":"ffffd"},
    "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.

提交回复
热议问题