Passing serialize array to struts2 action

孤街浪徒 提交于 2019-12-02 01:08:11

To send data with the POST request you should use data property like this

$.ajax({
   type: 'POST',
   url:'createcompany.action'
   data: 'jsonRequestdata='+data
   dataType: 'json',
   success: function(data){
      console.log(JSON.stringify(data.jsonRequestdata));
   }
});

To get the data in the action bean you need to use public setter

public void setJsonRequestdata(String data){ this.jsonRequestdata = data; }

To return data back to the success callback function use public getter

public String getJsonRequestdata(){ return this.jsonRequestdata; } 

To return JSON from the action use result type json.

<result type="json"><param name="includeProperties">jsonRequestdata</param></result>

Note, if you add json interceptor to the action config you can use JSON data in the request. Using Content-Type: "application/json" with the request will trigger Struts2 to parse the request and deserialize it to the action bean automatically.

Since I am using name,value I must get it by using name. Below is the working code

JSONArray jsonArr = (JSONArray) new JSONParser().parse(jsonRequestdata);

    for(int i=0;i<jsonArr.size();i++){
            JSONObject json=(JSONObject) jsonArr.get(i);   
            System.out.println("name=" + json.get("name"));
            System.out.println("value=" + json.get("value"));
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!