我web.xml在定义了一个filter,执行filter的时候,抛出一个timeout异常,希望struts2能捕获这个异常,然后跳转至login.jsp页面.可事实是在控制台打印如下:
008-11-24 11:03:08,714 (C03B200) org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/portal].[jsp] ERROR [http-8082-Processor24][StandardWrapperValve.java-253] - <Servlet.service() for servlet jsp threw exception>
my.exception.SessionTimeOutException: 未登陆或会话超时,请登陆.
at my.exception.SessionFilter.doFilter(SessionFilter.java:83)以下是相关代码,请各位帮忙看看怎么才能用struts来处理?谢谢!
-------------------------------------
public class SessionTimeOutException extends ServletException
{
    private String timeoutMsg = null;
    public SessionTimeOutException()
    {
        super();
    }
    public SessionTimeOutException(String _timeoutMsg)
    {
        super(_timeoutMsg);
        this.timeoutMsg = _timeoutMsg;
    }
    public String getTimeoutMsg()
    {
        return timeoutMsg;
    }
    public void setTimeoutMsg(String timeoutMsg)
    {
        this.timeoutMsg = timeoutMsg;
    }
}------------------------------
struts.xml:
...
<package name="exceptionMgmt" extends="struts-default" >
<global-results> 
<result name="sessionTimeOut">/login.jsp </result> 
</global-results>

<global-exception-mappings> 
<exception-mapping result="sessionTimeOut" exception="my.exception.SessionTimeOutException" /> 
</global-exception-mappings>
</package>
.....-------------------
public class SessionFilter implements Filter
{
    private HashSet notFilterUrl;   public void doFilter(ServletRequest servletRequest,
            ServletResponse servletResponse, FilterChain chain)
        throws IOException, ServletException
    {        if(timeout)
         {
           throw new SessionTimeOutException("未登陆或会话超时,请登陆.");
         }    }
}

解决方案 »

  1.   

    这明显是struts2没有捕获到这个异常。
      

  2.   

    是啊..
    我也觉得是这个问题,一个是在web.xml中配置的filter,抛出的异常一般由tomcat自行处理.我如果在filter中,其中sendRedirect至一个action,该action再跳转至login.jsp并且提示session超时,行的通.
    但是,在显示的页面上按F5刷新的时候,还是报sessionTimeOut(由于url重定向后始终是sessiontimeoutAction),这就不合理了.
      

  3.   

    你何时catch了这个异常?
    因为你的filter不是struts的某个action,所以它抛出的异常只有被容器捕获,而struts2不能捕获。struts2捕获局部异常是要在action中配置<exception-mapping>,全局异常不用配置,但是在action中的execute的方法中要用catch块来捕获。
      

  4.   

    我以为strut2会先自行捕获异常,如果不在struts.xml中定义的异常范围中,再会throw至容器来捕获这个异常.没想到此方法行不通.郁闷!!
    还在探索中!
      

  5.   

    sendRedirect是一个办法,刷新的问题,你可以结合getRequestDispacher().forward(request,response)试试。看看行不行!
      

  6.   

    搞定了..在filter中还是重定向至某个action;同时用ServletActionContext.getRequest().getSession().setAttribute("errMsg","something...");然后在Action中,取出这个msg后,再ServletActionContext.getRequest().getSession().removeAttribute("errMsg");这样,就可以解决刷新还是显示sessionTimeout的问题.