如何使用Ajax和Struts2将表单数据发送到ActionSupport

时间:2013-02-22 09:37:15

标签: java ajax json hibernate struts2

当我没有使用Ajax的时候,所有代码都很好。但是在这里使用ajax我得到了相同的公共字符串的null vale执行异常{String strAction = cartItemForm.getAction(); / *我在这里使用ajax调用使用null,但是简单的stuts2获取了<s:hidden name="action" value="Add" id="action" />中的动作值,这意味着获得add * / return“success”; }

这是jsp

  <script type="text/javascript">
  function submitAjax()  
  {  
   JSONobj = {cartItem:{
     description : $("description").val(),
     price :$("price").val(),
     action : $("action").val(),
     quantity :$("quantity").val(),
   } };
   JSONstr = JSON.stringify(JSONobj);

   jQuery.ajax(
    {
        type: 'POST', 
        url: 'Cart.action', 
        data:JSONstr,
        dataType: 'json',
        async: false ,
        contentType: 'application/json; charset=utf-8',
        success: function(){window.alert("done");}
    });
}  

</script>
<s:form>
  <s:hidden name="modelNo"     value="%{#attr.productId}"                    />  
  <s:hidden name="description" value="%{#attr.productName}" id="description" />
  <s:hidden name="price"       value="%{#attr.listprice}"   id="price"       />
  <s:hidden name="action"      value="Add"                  id="action"      />

  <s:submit value="add to cart" cssClass="submit_button" theme="simple"      />
</s:form>

这是struts.xml

 <action name="Cart" class="struts.cart.action.CartAction">
    <result type="json"/> 
 </action>

这是MY Action Class

  public class CartAction extends ActionSupport 
                        implements ModelDriven, Preparable, ServletRequestAware {
  private static final long serialVersionUID = -3670063011948002290L;
public  static final String ADD = "Add";
public  static final String DELETE = "Delete";
public  static final String UPDATE = "Update";  
public  static final String CART_SESSION = "Cart";

    private HttpServletRequest  request;
    private CartItemForm        cartItemForm;

    public String execute() throws Exception {
        String strAction = cartItemForm.getAction();//i am geting null here
        return "success";
    }

    public Object getModel() {
        // TODO Auto-generated method stub
        System.out.println("Cart getModel "+cartItemForm);
        return cartItemForm;
    }

    public void prepare() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Cart prepare "+cartItemForm);
        cartItemForm=new CartItemForm();
    }

    public void setServletRequest(HttpServletRequest  httpServletRequest) {
        // TODO Auto-generated method stub
        request = httpServletRequest;
    }
}

1 个答案:

答案 0 :(得分:2)

我不确定,如果我理解你的问题,那么这样做非常简单。这是jquery的可能解决方案:

<s:form id="myForm">
  <s:hidden name="modelNo" value="%{#attr.productId}"></s:hidden>  
  <s:hidden id="description" name="description" value="%{#attr.productName}"></s:hidden>
  <s:hidden id="price" name="price" value="%{#attr.listprice}"></s:hidden>
  <s:hidden id="action" name="action" value="Add"></s:hidden>
  <s:submit id="#submit" value="add to cart" cssClass="submit_button" theme="simple"></s:submit>
</s:form>

<script>
$("#submit").click(function(){
        var formData = $("#myForm").serialize(); //get all data from form
          //do the POST thingies
        $.ajax({
            type: "POST",
            url: "url_to_your_struts2_action",
            cache: false,
            data: formData,
            success: onSuccess,
            error: onError
        });

        return false;
});
</script>