java - Struts2: How do i get ServletRequest instance in ActionSupport - Stack Overflow

How do I get the ServletRequest instance in my action?I implemented ServletRequestAware but I am not ab

How do I get the ServletRequest instance in my action?

I implemented ServletRequestAware but I am not able to get request object in the action.

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>

I am making call using Ajax/JavaScript:

req.onreadystatechange = onReadyState;  
req.open(POST, Cart.action, false);  
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");  
req.send(JSONstr);

JSON object:

var data = { cartItem: {
     modelNo: $('#modelNo').val(),
     description: $('#description').val(),
     price: $('#price').val(),
     action: $('#action').val(),
     quantity: $('#quantity').val()
}};
JSONstr = JSON.stringify(data);

Action:

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

}   

How do I get the ServletRequest instance in my action?

I implemented ServletRequestAware but I am not able to get request object in the action.

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>

I am making call using Ajax/JavaScript:

req.onreadystatechange = onReadyState;  
req.open(POST, Cart.action, false);  
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");  
req.send(JSONstr);

JSON object:

var data = { cartItem: {
     modelNo: $('#modelNo').val(),
     description: $('#description').val(),
     price: $('#price').val(),
     action: $('#action').val(),
     quantity: $('#quantity').val()
}};
JSONstr = JSON.stringify(data);

Action:

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

}   
Share Improve this question edited Jan 17, 2014 at 21:41 Jaak Kütt 2,6564 gold badges33 silver badges40 bronze badges asked Feb 26, 2013 at 15:16 ved prakashved prakash 1241 gold badge3 silver badges10 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

try this

HttpServletRequest request = ServletActionContext.getRequest() ;
  1. Why do you need the servlet request? It's rare it's required.
  2. The reason ServletRequestAware isn't working is because you removed the interceptor that sets it into the action:
<action name="Cart" class="struts.cart.action.CartAction">
  <interceptor-ref name="json">
    <param name="contentType">application/json</param>
  </interceptor-ref>
  <result type="json"/> 
</action>   

When you set any interceptors in an action's configuration you must set all interceptors.

Here you've removed all the default interceptors, including "servletConfig" which sets the request for ServletRequestAware actions, and are running only the json interceptor.

You can use AjaxActionSupport instead ActionSupport in this way..

public class TestAjaxAction extends AJAXActionSupport {

//inside this you have perform method where you can get easily both object.

public void perform(HttpServletRequest request, HttpServletResponse response) throws AJAXActionException {

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743731233a4497469.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信