我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("未登陆或会话超时,请登陆.");
} }
}
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("未登陆或会话超时,请登陆.");
} }
}
解决方案 »
- Liferay5 如何集成SSH2 ????
- 开始学习java
- getParameter取不到值
- ★★★★★困惑已久的Spring问题终于解决!
- servlet中用cookie跟踪会话的一个记录访问页面次数的例子,不知道哪儿出问题了,请各位高手指教!
- 倒叙排列为什么rs.next()查不到最后一条记录?
- 【招聘】上海维西网络科技有限公司(VeryCD)
- 急。。Tomcat和Digester冲突的问题。。
- 请教ResourceBundle.getBundle("messages")一个问题
- 高分请教:关于JSP中的身份认证问题(安全性问题)
- 古怪问题请教
- 我的jsp页面永远保持第一次加载的状态怎么办
我也觉得是这个问题,一个是在web.xml中配置的filter,抛出的异常一般由tomcat自行处理.我如果在filter中,其中sendRedirect至一个action,该action再跳转至login.jsp并且提示session超时,行的通.
但是,在显示的页面上按F5刷新的时候,还是报sessionTimeOut(由于url重定向后始终是sessiontimeoutAction),这就不合理了.
因为你的filter不是struts的某个action,所以它抛出的异常只有被容器捕获,而struts2不能捕获。struts2捕获局部异常是要在action中配置<exception-mapping>,全局异常不用配置,但是在action中的execute的方法中要用catch块来捕获。
还在探索中!