我发送json数据并尝试进入struts2动作类

时间:2013-08-05 12:15:08

标签: jquery struts2

我的json数据是

{ “POSTDATA”:[{ “的itemId”: “mcw750”, “currentQty”: “90”, “saleQty”: “8”, “单价”: “100”, “salePrice”: “800”} ]}

var postData = JSON.stringify(data);

alert("The following data are sending to the server:\n"+postData);\\here i m getting all values..

$.ajax({
    type: "POST",
    url: "http://localhost:7001/Final_Subgrid2/jsonaction.action",
    dataType:"JSON",
    data:postData,
    traditional: true,
    contentType: "application/json; charset=utf-8",
    success: function(response, textStatus, xhr) {
    alert("success");
    },
    error: function(xhr, textStatus, errorThrown) {
    alert("error");
    }
});

在Action类中,我有使用setter和getter的公共List postData。 请告诉我为什么我在动作类中获取空数据。

我有员工pojo类。(itemId,currentQty,saleQty,unitPrice,totalPrice) 当我将更新的jqgrid数据发送到动作类时,它获取空值。 我的json数据没问题,ajax正在发送正确的数据(如firebug中所示)

我的动作映射是。            
            

我仍然得到空值,请指导我正确的方向

最后我解决了我的问题..谢谢所有人帮助我..

2 个答案:

答案 0 :(得分:0)

您不需要postdata的getter / setter,postdata项目将作为请求参数单独发送,因此itemId在requestmap中作为itemId。 Postdata不是作为json发送的。

在您的操作类中为您在postdata中发送的所有项目添加getter / setter

[get / set] ItemId,[get / set] CurrentQty,....

答案 1 :(得分:0)

我修改了我的struts.xml及其工作..

<action name="jsonaction" class="main.com.java.action.GetData" method="getJson">
  <interceptor-ref name="json">
    <param name="contentType">application/JSON</param>
    <param name="excludeNullProperties">true</param>
  </interceptor-ref>

  <result name="success" type="json"/>
</action>