System Dao, Service, Controller occurrence throws Exception is thrown up by the last referred to by the front end controller SpringMVC exception handler for exception handling, as shown below:
<! - Simple Mapping configuration exception handler ->
<the bean class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<! - default error view ->
<Property name = "defaultErrorView" value = "Forward: /error/errorMsg.jsp" />
<Property name = "exceptionMappings">
<Map>
! <- zero exception view ->
<entry Key = "an ArithmeticException" value = "Forward: / error / Arithmetic .jsp "/>
! <- cast exception view ->
<entry Key =" a ClassCastException "value =" Forward: /error/classCast.jsp "/>
</ Map>
</ Property>
</ the bean>
② implement Spring exception processing interface HandlerExceptionResolver customize your own exception handler
Define a class that implements an interface HandlerExceptionResolver, override method
public class MyExceptionResolver the implements the HandlerExceptionResolver { @Override public ModelAndView the resolveException (the HttpServletRequest Request, the HttpServletResponse Response, Object Handler, Exception EX) { // create ModelAndView ModelAndView ModelAndView = new new ModelAndView (); // determines what appears abnormal abnormality IF (EX the instanceof an ArithmeticException) { // If zero exception // Go to "Forward: /error/arithmetic.jsp" modelAndView.setViewName ( "Forward: /error/arithmetic.jsp" ); } the else IF (EX the instanceof a ClassCastException) { // If the type conversion exception modelAndView.setViewName ( "forward: /error/classCast.jsp" ); } the else { // other abnormality, jump to page exceptionPage modelAndView.setViewName ( "forward: /error/exceptionPage.jsp " ); } return ModelAndView; } }
Configuring exception handler
<bean id="exceptionResolver" class="com.itheima.exception.MyExceptionResolver"/>