日期:2014-05-19  浏览次数:20577 次

Struts2中的action实现SessionAware接口,得不到session.
Struts2中的action实现SessionAware接口,得不到session。报空指针异常。在网上看了一个文章,说是struts2的拦截器被覆盖了。所以得不到。但还是不太明白。求解

------解决方案--------------------
用 ServletConfig.getRequest.getSesion();
------解决方案--------------------
HttpSession session = ServletActionContext.getRequest().getSession();
------解决方案--------------------
如果是Struts2的默认拦截器被覆盖了的话,在拦截器栈中应该加上默认的拦截器栈,如下所示:
 
XML code

<interceptors>
           <interceptor name="dawInterceptor" class="org.DawInterceptor"/>
           <interceptor-stack name="myDefault">
           <interceptor-ref name="dawInterceptor"/>
           <interceptor-ref name="defaultStack"/>
           </interceptor-stack>
</interceptors>

------解决方案--------------------
SessionAware 这个接口只能获得Map类型的session 如果想得到session的话 给你提供两种方法:

1.直接用HttpSession = ServletActionContext.getRequest().getSession();
2.另一种就是继承ServletRequestAware 用来获取session