当我不使用 Ajax 时,使用相同的代码一切都很好。但是在这里使用 ajax 我得到了相同的 null 值 public String execute() throws Exception { 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>
这是我的行动课
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;
}
}