1.业务异常类
1.所以业务异常类派生于BusinessException基类。
2.原则上,要进行相同处理的异常分为一类,用ERROR_CODE标识不同。
3.出错信息统一写在errors.properties,以ERROR_CODE为主键,支持i18N,由基类提供默认的getMessage()函数。
参考BussinessException.java
2.Servlet规范里的异常控制
2.1按error-code统一定义错误页面
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
2.2按异常类型定义单独错误页面
<error-page>
<exception-type>org.sprngside.bookstore.UserNotFound</exception-type>
<location>/userNotFound.jsp</location>
</error-page>
2.3 在JSP里单独定义错误页面
<@ errorPage="error.jsp">
3.Spring MVC里的异常控制
spring-mvc可在xxx-serverlet.xml里定义default和 按Excepiton类型影射的错误页面, 和Servlet规范比,主要作了Spring特色的JSP路径转向和日志记录.参见bookstore-servlet.xml
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/error.jsp"/>
<property name="exceptionMappings">
<props>
<prop key="org.springside.framework.base.BusinessException">/businessError.jsp</prop>
</props>
</property>
</bean>
4. error.jsp的处理
error.jsp会同时处理jsp,servlet,和spring抛过来的异常
其中jsp的异常在exception 变量中.
servlet的异常在(Exception)request.getAttribute("javax.servlet.error.exception")
spring的异常在(Exception) request.getAttribute("exception")
使用 (String) request.getAttribute("javax.servlet.error.request_uri")获得 request_uri
使用 logger.error(exception.getMessage(), exception); 记录整个异常栈
5. spring配置
</bean> <!-- 异常处理类 --> <bean id="exceptionHandler" class="org.jeecgframework.core.common.exception.MyExceptionHandler" /> <!-- 系统错误转发配置[并记录错误日志] --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="500"></property> <!-- 默认为500,系统错误(error.jsp) --> <property name="defaultStatusCode" value="404"></property> <property name="statusCodes"><!-- 配置多个statusCode --> <props> <prop key="error">500</prop> <!-- error.jsp --> <prop key="error1">404</prop> <!-- error1.jsp --> </props> </property> <property name="exceptionMappings"> <props> <!-- 这里你可以根据需要定义N多个错误异常转发 --> <prop key="java.sql.SQLException">dbError</prop> <!-- 数据库错误(dbError.jsp) --> <prop key="org.springframework.web.bind.ServletRequestBindingException">bizError</prop> <!-- 参数绑定错误(如:必须参数没传递)(bizError.jsp) --> <prop key="java.lang.IllegalArgumentException">bizError</prop> <!-- 参数错误(bizError.jsp) --> <prop key="org.springframework.validation.BindException">bizError</prop> <!-- 参数类型有误(bizError.jsp) --> <prop key="java.lang.Exception">unknowError</prop> <!-- 其他错误为'未定义错误'(unknowError.jsp) --> </props> </property> </bean>
BusinessException.java
public class BusinessException extends RuntimeException { private static final long serialVersionUID = 1L; public BusinessException(String message){ super(message); } public BusinessException(Throwable cause) { super(cause); } public BusinessException(String message,Throwable cause) { super(message,cause); } }
MyExceptionHandler.java
import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.jeecgframework.core.common.model.json.AjaxJson; import org.jeecgframework.core.util.ExceptionUtil; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; /** * spring mvc异常捕获类 * */ @Component public class MyExceptionHandler implements HandlerExceptionResolver { private static final Logger logger = Logger .getLogger(MyExceptionHandler.class); public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { String exceptionMessage = ExceptionUtil.getExceptionMessage(ex); logger.error(exceptionMessage); Map<String, Object> model = new HashMap<String, Object>(); model.put("exceptionMessage", exceptionMessage); model.put("ex", ex); return new ModelAndView("common/error", model); } }