struts2 异常处理3板斧

板斧1:找不到action的错误

struts2 异常处理3板斧

在struts.xml中参考如下配置

 <struts>

     ...
<package name="default" namespace="/" extends="struts-default"> ... <default-action-ref name="index" /> ... <action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/home</param>
</result>
</action> </package> <include file="struts-home.xml" /> </struts>

这样,如果输入不存在的.action 路径,会直接重定向到index这个Action上,而index中指定的HelloWorld这个Action,在struts-home.xml中

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts> <package name="home" namespace="/home" extends="default"> <action name="HelloWorld_*" method="{1}" class="HelloWorldAction">
<result>/WEB-INF/views/home/HelloWorld.jsp</result>
</action> </package>
</struts>

注:struts.xml中节点出现的顺序,是有严格约定的,如果弄错顺序了,启动时,就会看到类似下面的异常

org.xml.sax.SAXParseException: The content of element type "package" must match

"(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*)".

即各节点的顺序为:

result-types -> interceptors -> default-interceptor-ref -> default-action-ref -> default-class-ref -> global-results -> global-exception-mappings -> action

板斧2:404/500之类的常规错误

呃,这个struts2处理不了,得靠web.xml搞定

     <error-page>
<error-code>404</error-code>
<location>/WEB-INF/common/error/404.jsp</location>
</error-page> <error-page>
<error-code>500</error-code>
<location>/WEB-INF/common/error/500.jsp</location>
</error-page>

板斧3:业务异常/常规(运行)异常

a) 定义业务异常 (这里简单弄一个土鳖的MyException意思一下)

package com.cnblogs.yjmyzz.exception;

public class MyException extends Exception {

    private static final long serialVersionUID = -8315871537638142775L;

    public MyException() {
super();
} public MyException(String message) {
super(message);
}
}

b) Action中,直接向外抛异常即可

     public String execute() throws Exception, MyException {

         //testException();

         testMyException();

         return SUCCESS;
} /*private void testException() throws Exception {
throw new Exception("normal exception");
}*/ private void testMyException() throws MyException {
throw new MyException("my exception");
}

c) 定义拦截器,处理异常

struts2中所有action的方法执行会先经常拦截器,所以拦截器是处理异常的好机机(比如:记录异常到日志文件、转换成友好异常信息)

 package com.cnblogs.yjmyzz.Interceptor;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.cnblogs.yjmyzz.exception.MyException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.*; public class ExceptionInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -6827886613872084673L;
protected Logger logger = LoggerFactory.getLogger(this.getClass());
protected Logger myexLogger = LoggerFactory.getLogger("my-exception"); @Override
public String intercept(ActionInvocation ai) throws Exception {
String result = null;
try {
logger.debug("ExceptionInterceptor.intercept() is called!");
result = ai.invoke();
} catch (MyException e) {
// 捕获自定义异常
myexLogger.error(ai.toString(), e);
ai.getStack().push(new ExceptionHolder(e));
result = "error";
} catch (Exception e) {
// 其它异常
logger.error(ai.toString(), e);
ai.getStack().push(new ExceptionHolder(e));
result = "error";
}
return result;
} }

解释一下:

ai.getStack().push(new ExceptionHolder(e)); 这一行的用途是将异常信息放入stack,这样后面的异常处理页面,就能显示异常详细信息

上面只是演示,将"业务异常MyException"与"常规异常Exception"分开处理,并且用不同的Logger实例来记录,这样就能将"业务异常"与"常规异常"分别记到不同的log文件中,对应的logback.xml参考配置:

 <?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="1800 seconds"
debug="false"> <property name="USER_HOME" value="logs" />
<property scope="context" name="FILE_NAME" value="test-logback" /> <timestamp key="byDay" datePattern="yyyy-MM-dd" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender> <appender name="file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${USER_HOME}/${FILE_NAME}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${USER_HOME}/${byDay}/${FILE_NAME}-${byDay}-%i.log.zip
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy> <triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level
%logger{150} - %msg%n
</pattern>
</encoder>
</appender> <appender name="exception-file"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${USER_HOME}/${FILE_NAME}_myexception.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${USER_HOME}/${byDay}/${FILE_NAME}-${byDay}-%i.log.zip
</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy> <triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-4relative [%thread] %-5level
%logger{150} - %msg%n
</pattern>
</encoder>
</appender> <logger name="com.cnblogs.yjmyzz" level="error" additivity="true">
<appender-ref ref="file" />
</logger> <logger name="my-exception" level="error" additivity="true">
<appender-ref ref="exception-file" />
</logger> <root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>

运行后,会生成二个日志文件,类似下图:(业务日志记录在test-logback_myexception.log中,常规运行异常记录在test-logback.log中)

struts2 异常处理3板斧

tips:如果还有更多的异常类型要处理(比如:SQL异常、Spring异常、网络连接异常等,参考上面的处理)。另:如果把3.b)中Action方法里的testMyException()注释掉,换成testException(),即抛出普通异常,则异常信息将记录到test-logback.log中

d) struts中拦截器配置,以及全局异常处理页面

 <?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <interceptors>
<interceptor name="myinterceptor"
class="com.cnblogs.yjmyzz.Interceptor.ExceptionInterceptor">
</interceptor> <interceptor-stack name="myStack">
<interceptor-ref name="myinterceptor" />
</interceptor-stack>
</interceptors> <default-interceptor-ref name="myStack" />
<default-action-ref name="index" /> <global-results>
<result name="error">/WEB-INF/common/error.jsp</result>
</global-results> <global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings> <action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/home</param>
</result>
</action> </package> <include file="struts-home.xml" />
<include file="struts-mytatis.xml" /> </struts>

解释一下:
13-21行,注册了自定义的拦截器,如果有更多的拦截器,19行后继续加即可。
23行,指定了默认的拦截器栈
26-28行,指定了全局的error返回页面
30-33行,指定了处理的异常类型

e) 通用error.jsp 代码

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %> <html>
<head><title>Simple jsp page</title></head>
<body>
<h3>Exception:</h3>
<s:property value="exception"/> <h3>Stack trace:</h3>
<pre>
<s:property value="exceptionStack"/>
</pre>
</body>
</html>

这样运行时,就会显示给用户一个很"低俗但通用"的错误页面:

struts2 异常处理3板斧

显然,直接把底层异常展示给用户是不好的做法(相当于直接告诉别人,今天你底裤的颜色),可以稍微装一下笔,只要改下拦截器:

 package com.cnblogs.yjmyzz.Interceptor;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.cnblogs.yjmyzz.exception.MyException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.*; public class ExceptionInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -6827886613872084673L;
protected Logger logger = LoggerFactory.getLogger(this.getClass());
protected Logger myexLogger = LoggerFactory.getLogger("my-exception"); @Override
public String intercept(ActionInvocation ai) throws Exception {
String result = null;
try {
logger.debug("ExceptionInterceptor.intercept() is called!");
result = ai.invoke();
} catch (MyException e) {
// 捕获自定义异常
myexLogger.error(ai.toString(), e);
// 转换成友好异常,并放入stack中
ai.getStack().push(
new ExceptionHolder(new Exception("业务繁忙,让我喘口气先!")));
result = "error";
} catch (Exception e) {
// 其它异常
logger.error(ai.toString(), e);
// 转换成友好异常,并放入stack中
ai.getStack().push(
new ExceptionHolder(new Exception("系统太累了,需要休息一下!")));
result = "error";
}
return result;
} }

这样,用户看到的信息就变了(当然,实际应用中,下面这个页面,建议请艺术大师美化一下)

struts2 异常处理3板斧

当然,也可以改变拦截器的返回string,比如业务错误,返回"biz-error",定位到业务错误的专用展示页面,常规错误返回"sys-error",返回 另一个专用错误处理页面(对应的struts.xml的全局错误配置也要相应修改)

小结:

经过以上处理,常见的异常(错误),比如:404/500、action路径不对、运行异常、业务异常等,即分门别类记录了详细日志(便于日后分析),也转换为友好信息提示给用户,同时还保证了系统健壮性。最后,对于程序员更重要的是,不用手动写try/catch之类的代码了,干活更轻松 (妈妈再也不担心我的异常了)

附:ajax的统一异常处理,请移步 Struts2、Spring MVC4 框架下的ajax统一异常处理

上一篇:【USACO 1.4】Mother's Milk


下一篇:Sass关于颜色函数的乐趣