SpringMVC 异常处理
异常处理类ExceptionHandler
package com.tobie.globalexception;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Tobieance
* @description 全局异常处理类
* @date 2023-09-15 10:11
*/
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* <h2>@ExceptionHandler:声明方法可以处理的异常类型</h2>
*
* @param e 要处理的异常
* @return {@link String}
*/
@ExceptionHandler(NameException.class)
public ModelAndView doNameException(Exception e){
ModelAndView modelAndView=new ModelAndView();
System.out.println("------doNameException------");
modelAndView.addObject("msg","用户名错误");
modelAndView.addObject("e",e);
modelAndView.setViewName("error");
e.printStackTrace();
return modelAndView;
}
@ExceptionHandler(PasswordException.class)
public ModelAndView doPasswordException(Exception e){
ModelAndView modelAndView=new ModelAndView();
System.out.println("------doPasswordException------");
modelAndView.addObject("msg","密码错误");
modelAndView.addObject("e",e);
modelAndView.setViewName("error");
e.printStackTrace();
return modelAndView;
}
/**
* 此方法用于处理任意异常(上面两个方法不能处理的异常)
*
* @param e 异常
* @return {@link ModelAndView}
*/
@ExceptionHandler
public ModelAndView doException(Exception e){
ModelAndView modelAndView=new ModelAndView();
System.out.println("------doException------");
modelAndView.addObject("msg","系统异常");
modelAndView.addObject("e",e);
modelAndView.setViewName("error");
e.printStackTrace();
return modelAndView;
}
}
ExceptionController异常处理控制器
package com.tobie.globalexception;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author Tobieance
* @description 异常处理控制器
* @date 2023-09-15 09:39
*/
@Controller
public class ExceptionController {
@RequestMapping("exception")
public String exception(String name, String password, ModelMap modelMap)throws Exception{
//用户名判断
if(!"tobie".equals(name)){
//抛出用户名错误异常
throw new NameException("用户名错误异常");
}
if(!"123".equals(password)){
//抛出密码错误异常
throw new PasswordException("密码错误异常");
}
modelMap.addAttribute("msg","账户名密码正确");
return "result";
}
}
Exception异常类
自定义用户名异常
package com.tobie.globalexception;
/**
* @author Tobieance
* @description 自定义用户名异常
* @date 2023-09-15 09:46
*/
public class NameException extends Exception{
public NameException(String message){
super(message);
}
}
密码异常
package com.tobie.globalexception;
/**
* @author Tobieance
* @description 密码异常
* @date 2023-09-15 09:48
*/
public class PasswordException extends Exception{
public PasswordException(String message){
super(message);
}
}
异常处理前端页面
<%--
Created by IntelliJ IDEA.
User: 87036
Date: 2023-09-15
Time: 09:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC 异常处理</title>
</head>
<body>
<form action="exception" method="post">
姓名:<input type="text" value="tobie" name="name"/><br/>
密码:<input type="password" value="123" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>