在J2EE / servlet Web应用程序中,您可以映射错误页面以指定异常,如下所示:
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/pages/404.jsp</location>
</error-page>
<error-page>
<exception-type>com.mkyong.web.exception.CustomException</exception-type>
<location>/WEB-INF/pages/error/custom_error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/WEB-INF/pages/generic_error.jsp</location>
</error-page>
上面的代码应该是自我探索的。 如果servlet容器中存在异常处理函数,为什么我们仍然需要使用Spring来处理异常?
通常,有两个原因:
- 自定义错误页面 – servlet容器将直接呈现错误页面; Spring允许您将模型或数据填充到错误页面,因此您可以自定义更用户友好的错误页面。
- 业务逻辑 – Spring允许您在呈现错误页面之前应用额外的业务逻辑,例如日志记录,审计等。
在本教程中,我们将向您展示两个示例来处理Spring中的异常。
- 对于Spring 2.x,我们在XML文件中使用
SimpleMappingExceptionResolver
。 - 对于Spring 3.x,我们可以通过
@ExceptionHandler
注释简化XML配置。
1. SimpleMappingExceptionResolver示例
查看目录结构。
自定义例外。
package com.mkyong.web.exception;
public class CustomGenericException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String errCode;
private String errMsg;
//getter and setter methods
public CustomGenericException(String errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
}
该控制器类仅抛出带有自定义错误代码和错误描述的CustomGenericException
。
package com.mkyong.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import com.mkyong.web.exception.CustomGenericException;
public class CustomerController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
throw new CustomGenericException("E888", "This is custom message - ABC");
}
}
查看下面的SimpleMappingExceptionResolver
:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong" />
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- Register the bean -->
<bean class="com.mkyong.web.controller.CustomerController" />
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.mkyong.wb.exception.CustomGenericException">
error/generic_error
</prop>
<prop key="java.lang.Exception">error/exception_error</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven />
</beans>
在上面,当
- 抛出CustomGenericException,它将映射到视图名称“ error / generic_error”。
- 引发任何其他异常,它将映射到视图名称“ error / exception_error”。
在JSP页面中,可以通过${exception}
访问异常实例。
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:if test="${not empty exception.errCode}">
<h1>${exception.errCode} : System Errors</h1>
</c:if>
<c:if test="${empty exception.errCode}">
<h1>System Errors</h1>
</c:if>
<c:if test="${not empty exception.errMsg}">
<h2>${exception.errMsg}</h2>
</c:if>
</body>
</html>
演示– http:// localhost:8080 / SpringMvcExample / customer
下载它– SpringMvc-SimpleMappingExceptionResolver-Example.zip (13KB)
2. @ExceptionHandler示例
从Spring 3.0开始,有一个新的注释@ExceptionHandler
可以简化XML配置。 以下是使用@ExceptionHandler
的等效版本。
package com.mkyong.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.mkyong.web.exception.CustomGenericException;
@Controller
public class CustomerController {
@RequestMapping(value = "/customer", method = RequestMethod.GET)
public ModelAndView getPages() throws Exception {
throw new CustomGenericException("E888", "This is custom message X");
}
@ExceptionHandler(CustomGenericException.class)
public ModelAndView handleCustomException(CustomGenericException ex) {
ModelAndView model = new ModelAndView("error/generic_error");
model.addObject("exception", ex);
return model;
}
@ExceptionHandler(Exception.class)
public ModelAndView handleAllException(Exception ex) {
ModelAndView model = new ModelAndView("error/exception_error");
return model;
}
}
在Spring XML文件中没有声明。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:annotation-driven />
</beans>
下载它– SpringMvc-ExceptionHandler-Example.zip (15KB)
注意
您可能对此Spring MVC @ExceptionHandler示例感兴趣
参考
翻译自: https://mkyong.com/spring-mvc/spring-mvc-exception-handling-example/