【问题标题】:send json Array to struts2 action using Ajax post使用 Ajax 发送 json 数组到 struts2 动作
【发布时间】:2013-10-09 11:58:39
【问题描述】:

在我的 struts2 应用程序中,我需要将 pojo 列表发送到 struts2 操作。但无法将其付诸实践。

这是我的 JavaScript 函数。

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();
                        }
         });

}

我的 struts2 操作

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

当我运行它时,我会在警报中获得请求的值:

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

如何在 struts2 动作中获得这个值?

【问题讨论】:

标签: jsp jquery arrays struts2-jquery


【解决方案1】:

我终于解决了这个问题。我在这里分享,这可能对其他人有用。

代替使用 ajax 发送数据这里我发送附加在 url 查询字符串中的数据。这里是示例。

                       $.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');

                        }
         });

这是行动的代码

    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;
    }

【讨论】:

    【解决方案2】:

    @Ravi Kant 首先,为什么要发送 json 字符串来执行操作?我的意思是为什么是json格式?有什么具体原因吗?您需要通过在 Action 文件中将 requests 声明为 String 变量来接收此 requests 变量,并使用 getter 和 setter 方法从表单中获取值。

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

    【讨论】:

    • 感谢 vinoth 的回复。我已经使用 getter 和 setter 进行了尝试,但它对我不起作用并且为 null。这就是我尝试使用 JSON 格式的原因。
    • 嘿,太好了,使用 javascript 数组将值发送到类似var requests = []; requests.push(xVal); 的操作,并在 java 中将此值作为字符串数组获取。试试这个,让我知道..
    【解决方案3】:

    例如:你可以试试这个方法

     $.ajax({
    
            type : 'post',      
    
            url : 'getsampleZone.action',
    
            data: "disctric ="+ value,
    
            dataType: 'json',    
    
            async: true ,
    
            success:function(x){
    
                alert("aasdsds");       
    
            }                       
    
    });
    

    在行动页面;

        private String disctric;
    
        System.out.println("disctric >>>>"+getDisctric());
    
        public String getDisctric() {
    
        return disctric;
    
        }
    
    public void setDisctric(String disctric) {
        this.disctric = disctric;
    }
    

    【讨论】:

      【解决方案4】:

      你可以这样使用

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

      然后在action类中为jsonAct生成setter getter

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

      我遇到了同样的问题,它得到了解决。

      【讨论】:

        猜你喜欢
        • 2015-01-08
        • 2013-10-24
        • 1970-01-01
        • 2013-10-11
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 2014-12-17
        相关资源
        最近更新 更多