我在做spring自带的宠物店程序,想把它改成struts2的东西,我们一般写action都会继承ActionSupport这个类,我的想法是觉得每次都要在程序里取request,session太麻烦,所以我写了一个BaseAction让他继承ActionSupport,在这里声名一下request,session并赋值,之后我自己的action继承BaseAction也就等于继承了ActionSupport并且把request,session注入进来了,但是我在自己的Action里居然取不到值。
BaseAction我是这样写的:protected Map session=ActionContext.getContext().getSession();
protected HttpServletRequest request = ServletActionContext.getRequest();
但是我如果在自己的action中这样写就可以,请指教一下,是什么原因,另外有没有人有办法,不要每次都要取request,session.

解决方案 »

  1.   

    写一个BaseAction,它继承ActionSupport,实现ServletRequestAware和SessionAware接口,你的业务Action直接继承BaseAction,直接使用request和session即可。例:public class BaseAction extends ActionSupport implements ServletRequestAware,
    SessionAware { protected HttpServletRequest request; protected Map<String, Object> session; public void setServletRequest(HttpServletRequest request) {
    this.request = request;
    }
    public void setSession(Map<String, Object> session) {
    this.session = session;
    }
    }
      

  2.   

    我按照一楼的做了,确实可以,不过我前面的写法为什么不可以呢?而且我在BaseAction声明了另外一个叫petStore的变量,给了set/get方法,配置文件也注入了,也是在我的Action中取不到值,这应该是和我取不到request的原因是一样的吧,这是什么原因呢?
      

  3.   

    HttpSession s=ServletActionContext.getRequest().getSession() 试试这个
      

  4.   


    request都取不到,再取session会空指针异常的。
      

  5.   

    我调试的时候发现petStore已经注入成功了,怎么我在我的Action里用的时候就变成的null了呢,ActionContext.getContext().getSession();方法为什么在BaseAction中不起作用呢?哪们给解答一下啊,谢谢
      

  6.   

    public class BaseAction  implements ServletRequestAware,ServletResponseAware,SessionAware{
    /**
     * 非Ioc方式创建
     */
    // public HttpServletRequest getRequest(){
    // return ServletActionContext.getRequest();
    // }
    // public HttpServletResponse getResponse(){
    // return ServletActionContext.getResponse();
    // }
    // public HttpSession getSession(){
    // return ServletActionContext.getRequest().getSession();
    // }

    /**
     * Ioc方式创建
     */
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected Map session;
    protected final String INDEX="index";
    protected final String ERROR="error";
    protected final String SUCCESS="success";
    protected final String LOGIN="login";
    public void setServletRequest(HttpServletRequest request) {
    this.request=request;
    }
    public void setServletResponse(HttpServletResponse response) {
    this.response=response;
    }
    public void setSession(Map m) {
    this.session=m;
    }

    }
      

  7.   


    这个成:
       /**
        * 获得请求对象HttpServletRequest
        * 
        * @return request
        */
      protected final HttpServletRequest getRequest()
       {
          return ServletActionContext.getRequest();
       }
       /**
        * 获得struts2的session对象
        * 
        * @return Map<String, Object>
        */
       protected final Map<String, Object> getSession()
       {
          return getActionContext().getSession();
       }
      

  8.   

       /**
        * 获得上下文对象ActionContext
        * 
        * @return actionContext
        */
       protected final ActionContext getActionContext()
       {
          return (actionContext == null) ? actionContext = ActionContext.getContext() : actionContext;
       }
      

  9.   

    LZ的用法就好像在Action中定义个一属性并初始化值。不调用该属性属性值始终不变。