标签:body Servle content view display min alt back 空指针
@Controller @RequestMapping("/error") public class ErrorController { @RequestMapping("/firstRequest") public String firstRequest(){ //模拟异常 int result=5/0; return "index"; } }
<!--系统异常处理器--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"></property> <property name="exceptionAttribute" value="ex"></property> </bean>
(当空指针错误走error,名称错误走nameError,年龄错误走ageError)
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
@RequestMapping("/secondRequest") public String secondRequest(String userName,Integer userAge) throws NameException, AgeException { if (!userName.equals("admin")){ throw new NameException("用户名错误!"); } if (userAge>99){ throw new AgeException("年龄不符合规定!"); } return "success"; }
<!--系统异常处理器--> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"></property> <property name="exceptionAttribute" value="ex"></property> <!--定制异常--> <property name="exceptionMappings"> <props> <prop key="com.springmvc.exception.NameException">nameError</prop> <prop key="com.springmvc.exception.AgeException">ageError</prop> </props> </property> </bean>
package com.springmvc.exception;
public class NameException extends Exception{
public NameException() {
}
public NameException(String message) {
super(message);
}
}
package com.springmvc.exception;
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
/** * 自定义异常处理器 */ public class MyHandlerException implements HandlerExceptionResolver { @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ModelAndView modelAndView=new ModelAndView(); //如果发生异常,给一个默认异常处理页面 modelAndView.setViewName("error"); modelAndView.addObject("ex",ex); //如果发生Name异常,则跳转到Name异常页面 if (ex instanceof NameException){ modelAndView.setViewName("nameError"); } //如果发生age异常,则跳转到age异常页面 if (ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; } }
<!--将自定义异常处理器注册到Spring容器--> <bean class="com.springmvc.exception.MyHandlerException"/>
package com.springmvc.exception;
public class NameException extends Exception{
public NameException() {
}
public NameException(String message) {
super(message);
}
}
package com.springmvc.exception;
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
@ExceptionHandler public ModelAndView exceptionHandler(Exception ex){ ModelAndView modelAndView=new ModelAndView(); //如果发生异常,给一个默认异常处理页面 modelAndView.setViewName("error"); modelAndView.addObject("ex",ex); //如果发生Name异常,则跳转到Name异常页面 if (ex instanceof NameException){ modelAndView.setViewName("nameError"); } //如果发生age异常,则跳转到age异常页面 if (ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; }
package com.springmvc.exception;
public class NameException extends Exception{
public NameException() {
}
public NameException(String message) {
super(message);
}
}
package com.springmvc.exception;
public class AgeException extends Exception{
public AgeException() {
}
public AgeException(String message) {
super(message);
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>登陆页面</title> </head> <body> <form action="/error/secondRequest" method="post"> 姓名:<input type="text" name="userName"/> 年龄:<input type="text" name="userAge"/> <input type="submit" value="提交"/> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>姓名错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>年龄错误</title> </head> <body> <h1>您输入的信息:${ex.message}</h1> </body> </html>
/** * 全局注解处理 */ @ControllerAdvice public class ExceptionControllerAdvice { @ExceptionHandler public ModelAndView exceptionHandler(Exception ex){ ModelAndView modelAndView=new ModelAndView(); //如果发生异常,给一个默认异常处理页面 modelAndView.setViewName("error"); modelAndView.addObject("ex",ex); //如果发生Name异常,则跳转到Name异常页面 if (ex instanceof NameException){ modelAndView.setViewName("nameError"); } //如果发生age异常,则跳转到age异常页面 if (ex instanceof AgeException){ modelAndView.setViewName("ageError"); } return modelAndView; } }
标签:body Servle content view display min alt back 空指针
原文地址:https://www.cnblogs.com/Zzzzn/p/11835160.html