2013-10-09 61 views
1

在我的struts2應用程序中,我有要求將pojo列表發送到struts2動作。但無法獲得行動的方式。將json數組發送到使用Ajax的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; 
    } 

當我運行此我警告get請求的值:

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

我如何獲得Struts2的行動這個價值?

+0

你可以打印你的輸出,因爲我有問題http://stackoverflow.com/questions/22062180/sending-array-from-jquery-to-servlet – haripcce

回答

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

@Ravi康德首先你爲什麼要發送json字符串的行動?我的意思是爲什麼json格式?任何具體原因?您需要通過在Action文件中將請求聲明爲String變量來接收此請求變量,並使用getter和setter方法從form獲取值。

private String requests; 

public String getRequests() { 
    return requests; 
} 

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

感謝您的回覆vinoth.I已經嘗試過使用getter和setter,但是它不適用於我,並得到null。這就是爲什麼我試圖使用JSON格式。 –

+0

嘿,多數民衆贊成在偉大的,使用JavaScript數組發送值的行爲,如'var requests = []; requests.push(xVal);'並將此值作爲String中的String數組獲取。試試這個,讓我知道.. –

0

。例如代碼:你可以嘗試這樣

$.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; 
} 
0

您可以使用這樣

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

,然後生成動作類jsonAct二傳手吸氣

private String jsonAct; 




public String getJsonAct() { 
    return jsonAct; 
} 

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

我daced同樣的問題,並得到了解決。

相關問題