定义
捕获异常并定位到指定视图。
ps:从这给类的原型,可以帮助我们思考异常如何以日志形式记录,可以再此类的基础上进行扩展。这个待日后有慢慢琢磨。
使用
1.局部exception配置使用
Action源码:
- public class SourceCoreAction {
-
- public String error() throws ClassNotFoundException {
- System.out.println("do the error!");
- Class.forName("xxx");
- return "success";
- }
-
- }
struts-xxx.xml配置:
- <span style="white-space:pre"> </span><action name="error" class="com.company.strutstudy.web.action.core.SourceCoreAction"
- method="error">
- <interceptor-ref name="exception"></interceptor-ref>
- <result name="input">/core/exception.jsp</result>
- <result name="success">/core/ok.jsp</result>
- <exception-mapping result="input" exception="java.lang.Exception"></exception-mapping>
- </action>
2.全局exception配置使用
Action源码不变,参考局部exception
struts-xxx.xml配置:
- <struts>
- <package name="exception" extends="struts-default" abstract="true">
- <global-results>
- <result name="input">/core/exception.jsp</result>
- </global-results>
- <global-exception-mappings>
- <exception-mapping result="input" exception="java.lang.Exception"></exception-mapping>
- </global-exception-mappings>
- </package>
- <package name="core" extends="exception">
- <action name="error"class="com.company.strutstudy.web.action.core.SourceCoreAction" method="error">
- <interceptor-ref name="exception"></interceptor-ref>
- <result name="success">/core/ok.jsp</result>
- </action>
- </package>
- </struts>
源码阅读
- public class ExceptionMappingInterceptor extends AbstractInterceptor {
-
- protected static final Log log = LogFactory.getLog(ExceptionMappingInterceptor.class);
- protected Log categoryLogger;
- protected boolean logEnabled = false;
- protected String logCategory;
- protected String logLevel;
-
- ....getset省略
- public String intercept(ActionInvocation invocation) throws Exception {
- String result;
-
-
-
- try {
- result = invocation.invoke();
- } catch (Exception e) {
-
- if (logEnabled) {
- handleLogging(e);
- }
-
- List exceptionMappings = invocation.getProxy().getConfig().getExceptionMappings();
-
- String mappedResult = this.findResultFromExceptions(exceptionMappings, e);
- if (mappedResult != null) {
- result = mappedResult;
-
- publishException(invocation, new ExceptionHolder(e));
- } else {
- throw e;
- }
- }
-
- return result;
- }
-
-
-
-
-
-
- protected void handleLogging(Exception e) {
- if (logCategory != null) {
- if (categoryLogger == null) {
-
- categoryLogger = LogFactory.getLog(logCategory);
- }
- doLog(categoryLogger, e);
- } else {
- doLog(log, e);
- }
- }
-
-
-
-
-
-
-
- protected void doLog(Log logger, Exception e) {
- if (logLevel == null) {
- logger.debug(e.getMessage(), e);
- return;
- }
- if ("trace".equalsIgnoreCase(logLevel)) {
- logger.trace(e.getMessage(), e);
- } else if ("debug".equalsIgnoreCase(logLevel)) {
- logger.debug(e.getMessage(), e);
- } else if ("info".equalsIgnoreCase(logLevel)) {
- logger.info(e.getMessage(), e);
- } else if ("warn".equalsIgnoreCase(logLevel)) {
- logger.warn(e.getMessage(), e);
- } else if ("error".equalsIgnoreCase(logLevel)) {
- logger.error(e.getMessage(), e);
- } else if ("fatal".equalsIgnoreCase(logLevel)) {
- logger.fatal(e.getMessage(), e);
- } else {
- throw new IllegalArgumentException("LogLevel [" + logLevel + "] is not supported");
- }
- }
-
- private String findResultFromExceptions(List exceptionMappings, Throwable t) {
- String result = null;
-
-
- if (exceptionMappings != null) {
- int deepest = Integer.MAX_VALUE;
- for (Iterator iter = exceptionMappings.iterator(); iter.hasNext();) {
- ExceptionMappingConfig exceptionMappingConfig = (ExceptionMappingConfig) iter.next();
- int depth = getDepth(exceptionMappingConfig.getExceptionClassName(), t);
- if (depth >= 0 && depth < deepest) {
- deepest = depth;
- result = exceptionMappingConfig.getResult();
- }
- }
- }
-
- return result;
- }
-
-
-
-
-
-
-
-
-
- public int getDepth(String exceptionMapping, Throwable t) {
- return getDepth(exceptionMapping, t.getClass(), 0);
- }
-
- private int getDepth(String exceptionMapping, Class exceptionClass, int depth) {
- if (exceptionClass.getName().indexOf(exceptionMapping) != -1) {
-
- return depth;
- }
-
- if (exceptionClass.equals(Throwable.class)) {
- return -1;
- }
- return getDepth(exceptionMapping, exceptionClass.getSuperclass(), depth + 1);
- }
-
-
-
-
-
-
-
-
- protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {
- invocation.getStack().push(exceptionHolder);
- }
- }