springmvc-210806-02—异常处理
异常处理步骤
1. 新建maven项目
2. 加入依赖
3. 新建一个自定义异常类MyException,在定义它的子类,NameException,AgeException
4. 在controller抛出NameException,AgeException
5. 创建一个普通类,作为全局异常处理类
1) 在类上面加上注解@ControllerAdvice
2) 在类中定义方法,方法上面加入注解@ExceptionHandler
6. 创建处理异常的视图页面
7. 创建springmvc的配置文件
1) 组件扫描器,扫描@Controller注解
2) 组件扫描器,扫描@ControllerAdvice所在的包名
3) 声明注解驱动
演示异常处理
MyException.java(自定义异常类)
package com.bgy.exception;
public class MyException extends Exception{
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}
NameException.java(自定义异常子类)
package com.bgy.exception;
public class NameException extends MyException{
public NameException() {
super();
}
public NameException(String message) {
super(message);
}
}
AgeException.java(自定义异常子类)
package com.bgy.exception;
public class AgeException extends MyException{
public AgeException() {
super();
}
public AgeException(String message) {
super(message);
}
}
GlobalExceptionHandler.java(全局异常处理类)
package com.bgy.handler;
import com.bgy.exception.AgeException;
import com.bgy.exception.NameException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = NameException.class)
public ModelAndView doNameException(Exception exception) {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","姓名必须是bgy,其他用户不得访问");
mv.addObject("ex",exception);
mv.setViewName("nameError");
return mv;
}
@ExceptionHandler(value = AgeException.class)
public ModelAndView doAgeException(Exception exception) {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","年龄不得大于100");
mv.addObject("ex",exception);
mv.setViewName("ageError");
return mv;
}
@ExceptionHandler()
public ModelAndView doDefaultErrorException(Exception exception) {
ModelAndView mv = new ModelAndView();
mv.addObject("msg","其他异常");
mv.addObject("ex",exception);
mv.setViewName("defaultError");
return mv;
}
}
MyController.java(手动定义异常,用来测试)
package com.bgy.controller;
import com.bgy.exception.AgeException;
import com.bgy.exception.MyException;
import com.bgy.exception.NameException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MyController {
@RequestMapping("/some.do")
public ModelAndView doSome(String name,Integer age) throws MyException {
ModelAndView mv = new ModelAndView();
if (!"bgy".equals(name)) {
throw new NameException("姓名不正确.......");
}
if (age==null || age > 100 ) {
throw new AgeException("年龄太大了.......");
}
mv.addObject("uname",name);
mv.addObject("uage",age);
mv.addObject("msg","异常处理");
mv.setViewName("show");
return mv;
}
}
index.jsp(主界面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath = request.getScheme() + "://" +
request.getServerName() + ":" +
request.getServerPort() +
request.getContextPath() + "/";
%>
<html>
<head>
<title>异常处理</title>
<base href="<%=basePath%>"/>
</head>
<body>
<form action="some.do" method="post">
<span>异常处理</span>
<br/>
<span>姓名:</span><input type="text" name="name"/>
<br/>
<span>年龄:</span><input type="text" name="age"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
show.jsp(没有异常显示页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>show.jsp</title>
</head>
<body>
WEB-INF/viw/show.jsp
<br/>
${msg}
<br/>
姓名:${uname}
<br/>
年龄:${uage}
</body>
</html>
ageError.jsp(AgeException异常页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>ageError</title>
</head>
<body>
ageError错误页面
<br/>
提示信息:${msg}
<br/>
系统异常消息:<br/>${ex.message}
</body>
</html>
nameError.jsp(NameException异常页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>nameError</title>
</head>
<body>
nameError错误页面
<br/>
提示信息:${msg}
<br/>
系统异常消息:<br/>${ex.message}
</body>
</html>
DefaultException.jsp(其他异常页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>defaultError</title>
</head>
<body>
defaultError错误页面
<br/>
提示信息:${msg}
<br/>
系统异常消息:<br/>${ex.message}
</body>
</html>
我的项目目录
|