如何ServletRequest
在我的操作中获取实例?
我实现ServletRequestAware
了,但我无法在操作中获取请求对象。
struts.xml
<package name="default" extends="struts-default,json-default">
<action name="Cart"
class="struts.cart.action.CartAction">
<interceptor-ref name="json">
<param name="contentType">application/json</param>
</interceptor-ref>
<result type="json"/>
</action>
</package>
我正在使用 Ajax/JavaScript 拨打电话:
req.onreadystatechange = onReadyState;
req.open(POST, Cart.action, false);
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.send(JSONstr);
JSON对象:
var data = { cartItem: {
modelNo: $('#modelNo').val(),
description: $('#description').val(),
price: $('#price').val(),
action: $('#action').val(),
quantity: $('#quantity').val()
}};
JSONstr = JSON.stringify(data);
行动:
public class CartAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private Map cartItem = new HashMap();
public String execute() throws Exception {
System.out.println("request " + cartItem); // getting here cartitem
System.out.println("request " + request); // request null
}
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = httpServletRequest;
}
public Map getCartItem() {
return cartItem;
}
public void setCartItem(Map cartItem) {
this.cartItem = cartItem;
}
}