Exception handling Struts 2-如何将Exception类型的异常重定向到特定页面,但不处理特定的异常?
我希望将所有类型为Exception handling Struts 2-如何将Exception类型的异常重定向到特定页面,但不处理特定的异常?,exception-handling,struts2,struts,Exception Handling,Struts2,Struts,我希望将所有类型为Exception的错误重定向到结果“error”。为此,我做了以下工作: <global-exception-mappings> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> 但是我不想处理一个特殊的异常,特别是org.
Exception
的错误重定向到结果“error”。为此,我做了以下工作:
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
但是我不想处理一个特殊的异常,特别是org.springframework.security.access.AccessDeniedException
,应该允许它进一步传播。如何实现这一点?使用instanceof
操作符,并从异常处理程序中重新调用所需的异常
我用拦截器管理它(这里是我用来尝试的):
以下是struts.xml(为了节省查找struts dtd的时间):
我按照你说的做了,它说“检测到无限递归”,这是我们所期望的,因为我们不断抛出我们正在捕获的同一个异常,它不断被一次又一次地捕获。我不想让Struts处理这个特殊的异常。很抱歉,重新思考一下。您已经在拦截器堆栈的顶部添加了Rethrower拦截器,因此默认的ExceptionMappingInterceptor将有机会在此之前执行,它甚至会将RuntimeException映射到“error.jsp”。Rethrower甚至可能没有机会看到这个异常。我说的对吗?不,两者都将执行(这可能不是您想要的,但通常这会让您向用户显示错误,然后我假设异常不是更改视图,而是执行一些清理)。我认为ExceptionMappingInterceptor以某种方式将异常放入valuestack(我没有检查这个)。因为rethrower会检查valuestack中是否存在键控为“exception”的对象,所以颠倒顺序将不起作用。如果您不想同时执行这两项操作,那么我认为您需要实现自己的ExceptionMappingInterceptor并替换它。与问题相关:
package com.kenmcwilliams.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Rethrower implements Interceptor{
@Override
public void destroy() {
}
@Override
public void init() {
}
@Override
public String intercept(ActionInvocation invocation){
System.out.println("Start rethrower!");
String result = "success";
try {
result = invocation.invoke();
} catch (Exception ex) {
Logger.getLogger(Rethrower.class.getName()).log(Level.SEVERE, null, ex);
}
Object exception = ActionContext.getContext().getValueStack().findValue("exception");
if (exception instanceof RuntimeException){
System.out.println("DOING RETHROW!");
RuntimeException e = (RuntimeException)exception;
throw e;
}
System.out.println("After rethrower!");
return result;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="kenmcwilliams" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="rethrower" class="com.kenmcwilliams.interceptor.Rethrower"/>
<interceptor-stack name="rethrow-stack">
<interceptor-ref name="rethrower"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<global-results>
<result name="error" >/WEB-INF/content/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="mybomb" class="com.kenmcwilliams.kensocketchat.action.Bomb">
<interceptor-ref name="rethrow-stack"/>
<result>/WEB-INF/content/bomb.jsp</result>
</action>
</package>
</struts>
package com.kenmcwilliams.kensocketchat.action;
import com.opensymphony.xwork2.ActionSupport;
public class Bomb extends ActionSupport{
@Override
public String execute() throws Exception{
throw new RuntimeException();
}
}