标签:html tle img and 对象 dtd 自动跳转 ram 跳转
springboot已经内嵌了异常处理的机制,如果发生错误会自动跳转到error界面,默认的error界面为:
我们可以自定义异常界面,但是需要在 src/main/resources/templates 目录下创建 error.html 页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Title</title> </head> <body> <!--高版本可以通过${message}获取异常信息--> <span th:text="${message}"></span> </body> </html>
contorller:
@Controller public class UsersController { @RequestMapping("showInfo") public String showIno() { String str = null; str.length(); return "OK"; } @RequestMapping("showInfo2") public String showIno2() { int a = 10 / 0; return "OK"; }
//value 是一个数组,可以添加多个异常,但只能指向同一个异常页面 @ExceptionHandler(value = {java.lang.NullPointerException.class}) public ModelAndView nullPointExceptionHandler(Exception e) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("error", e.toString()); modelAndView.setViewName("error1"); return modelAndView; } @ExceptionHandler(value = {java.lang.ArithmeticException.class}) public ModelAndView ArithmeticExceptionHandler(ArithmeticException e) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("error", e.toString()); modelAndView.setViewName("error2"); return modelAndView; }
error1.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Title</title> </head> <body> 空指针异常 <span th:text="${error}"></span> </body> </html>
error2:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Title</title> </head> <body> <span th:text="${error}"></span> </body> </html>
GlobalException:
/* * 全局异常处理类 * */ @ControllerAdvice public class GlobalException { @ExceptionHandler(value = {java.lang.NullPointerException.class}) public ModelAndView nullPointExceptionHandler(Exception e) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("error", e.toString()); modelAndView.setViewName("error1"); return modelAndView; } @ExceptionHandler(value = {java.lang.ArithmeticException.class}) public ModelAndView ArithmeticExceptionHandler(ArithmeticException e) { ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("error", e.toString()); modelAndView.setViewName("error2"); return modelAndView; } }
/* * 全局异常 * */ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.Properties; @Configuration public class GlobalException2 { /* * 此方法返回值必须是SimpleMappingExceptionResolver类型 * */ @Bean public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver(); Properties properties = new Properties(); /* *参数一:异常类型,并且是全名 * 参数二:视图名称 * */ properties.put("java.lang.NullPointerException", "error3"); properties.put("java.lang.ArithmeticException", "error4"); resolver.setExceptionMappings(properties); return resolver; } }
error3.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Title</title> </head> <body> java.lang.NullPointerException </body> </html>
/* * 自定义HandlerExceptionResolver对象处理异常 * 必须实现HandlerExceptionResolver接口 * */ @Configuration public class GlobalException3 implements HandlerExceptionResolver { /* * httpServletRequest产生异常的请求对象 * httpServletResponse产生异常的响应对象 * handler此次产生异常的handler对象 * e 产生异常的异常对象 * */ @Override public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception e) { ModelAndView mv = new ModelAndView(); //判断不同异常类型,做不同视图跳转 if (e instanceof NullPointerException) { mv.setViewName("error5"); } if(e instanceof ArithmeticException){ mv.setViewName("error6"); } mv.addObject("error",e.toString()); return mv; } }
标签:html tle img and 对象 dtd 自动跳转 ram 跳转
原文地址:https://www.cnblogs.com/activestruggle/p/12891694.html