سؤال

In my struts2 application, I have requirement to send list of pojo to struts2 action. But unable to get the way to get that on action.

Here is my JavaScript function.

function go() {
    var requests = Array();
    var url='testUrl';
      $('.innertable').each(function(index){
         var form= $(this).closest('form');
         if(index!=0)
             {
             var xVal=form.find("#xVal"+index).val();
             }
             else
              {
             var xVal=form.find("#xVal"+index).val();
              } 
         var testPojo = {
                 xVal:xVal
             }
         requests.push(testPojo);
         });
         alert('======='+JSON.stringify(requests));
                      $.ajax({
                        url: url,
                        data: JSON.stringify(requests),
                        contentType: "application/json; charset=utf-8",
                        type: 'POST',
                        success: function(data){
                       //success code
                        },
                        error: function(xhr, status, error) {
                            alert(xhr.responseText);
                             window.location.reload();
                        }
         });

}

My struts2 action

public String testUrl()
    {
        //here what code i can use to get List of pojo
        return SUCCESS;
    }

When I run this I get request's value in alert:

[{"xVal":"$3,000"},{"xVal":"$5,000"}]

How can I get this value in struts2 action?

هل كانت مفيدة؟

المحلول

Finally i solved the issue.I am sharing here which might be useful for any one else.

In place of sending data using ajax Here i am sending data appending in query string of url.Here is sample.

                       $.ajax({
                        url: url+"?data="+JSON.stringify(requests),
                        contentType: "application/json; charset=utf-8",
                        type: 'POST',
                        success: function(data){
                        //success code
                        },
                        error: function(xhr, status, error) {
                            alert(xhr.responseText);
                            alert('there is some problem in updating data');

                        }
         });

This is the code of action

    public String testUrl()
    {

        String data = request.getParameter("data");

        System.out.println("data is :"+data);
        List<TestPojo> testPojos=new ArrayList<TestPojo>();
        try{
            JSONArray jsonArray1 = new JSONArray(data);
            for (int i = 0; i < jsonArray1.length(); i++) {
                JSONObject jsonObject = jsonArray1.getJSONObject(i);

                TestPojo testPojo = new TestPojo();

                testPojo.setTestField(jsonObject.get("testField").toString());

                testPojos.add(testPojo);    
            }
           //additional code here 

        }catch (Exception e) {
            System.out.println("error");
        }

        return SUCCESS;
    }

نصائح أخرى

@Ravi Kant First why you are sending json string to action? I mean why json format? Any specific reasons? You need to receive this requests variable by declaring requests as String variable in Action file and use getter and setter method for get the value from form.

private String requests;

public String getRequests() {
    return requests;
}

public void SetRequests(String requests) {
    this.requests = requests;
}

For example : you can try this way

 $.ajax({

        type : 'post',      

        url : 'getsampleZone.action',

        data: "disctric ="+ value,

        dataType: 'json',    

        async: true ,

        success:function(x){

            alert("aasdsds");       

        }                       

});

In Action Page ;

    private String disctric;

    System.out.println("disctric >>>>"+getDisctric());

    public String getDisctric() {

    return disctric;

    }

public void setDisctric(String disctric) {
    this.disctric = disctric;
}

You can use like this

var jsonStr = JSON.stringify(requests);    
                      $.ajax({
                        url: url,
                        data: "jsonAct="+jsonStr ,
                        contentType: "application/json; charset=utf-8",
                        type: 'POST',
                        success: function(data){
                       //success code
                        }

and then generate setter getter for jsonAct in action class

private String jsonAct;




public String getJsonAct() {
    return jsonAct;
}

public void setJsonAct(String jsonAct) {
    this.jsonAct = jsonAct;
}

I daced the same issue and it got resolved.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top