1.基于XML配置

  1. <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  2. <property name="exceptionMappings">
  3. <props>
  4. <!--跳转为fail.html-->
  5. <prop key="java.lang.ArithmeticException">fail</prop>
  6. </props>
  7. </property>
  8. <!--异常ex值为e.getMessage会放入请求域中-->
  9. <property name="exceptionAttribute" value="ex"></property>
  10. </bean>

2.基于代码

  1. @ExceptionHandler({ArithmeticException.class,NullPointerException.class})
  2. public String handlerEx(Exception ex,Model model){
  3. model.addAttribute("ex",ex);
  4. return "fail";
  5. }