@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)html
<%@ 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; } }