SSM-SpringMVC-25:SpringMVC异常顶级之自定义异常解析器

释放双眼,带上耳机,听听看~!

 

 

————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-

 

 

上篇博客相信大家也看到了,自定义异常,用了SimpleMappingExceptionResolver这个解析器,本次要讲的是自定义异常解析器:

自己定义的异常解析器,实现了HandlerExceptionResolver,一会再在xml中配置一道,我把需要改或者新建的三处放在前面,下面的与上篇博客的一样(不过我也放上来),看着来把,活学活用

 

案例

1,自定义异常解析器MyHandlerExceptionResolver:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1package cn.dawn.day17selfexceptionresolver.resolver;
2
3import cn.dawn.day17selfexceptionresolver.userexception.UserageException;
4import cn.dawn.day17selfexceptionresolver.userexception.UsernameException;
5import org.springframework.web.servlet.HandlerExceptionResolver;
6import org.springframework.web.servlet.ModelAndView;
7
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11/**
12 * Created by Dawn on 2018/3/30.
13 */
14public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
15    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
16        ModelAndView modelAndView=new ModelAndView();
17        /*返回的异常对象*/
18        modelAndView.addObject("ex",ex);
19        /*判断去那个页面*/
20        if(ex instanceof UsernameException){
21            modelAndView.setViewName("name");
22        }
23        if(ex instanceof UserageException){
24            modelAndView.setViewName("age");
25        }
26
27        return modelAndView;
28    }
29}
30

 

2.自己的xml大配置文件:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xmlns:mvc="http://www.springframework.org/schema/mvc"
5       xmlns:context="http://www.springframework.org/schema/context"
6       xsi:schemaLocation="http://www.springframework.org/schema/beans
7        http://www.springframework.org/schema/beans/spring-beans.xsd
8        http://www.springframework.org/schema/mvc
9        http://www.springframework.org/schema/mvc/spring-mvc.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd">
12
13    <!--包扫描器-->
14    <context:component-scan base-package="cn.dawn.day17selfexceptionresolver"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day17/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21    <!--自己的异常解析器-->
22    <bean class="cn.dawn.day17selfexceptionresolver.resolver.MyHandlerExceptionResolver"></bean>
23
24</beans>
25

 

3.讲web.xml中央调度器的上下文配置位置改为你现在这个配置xml

 

下面的上篇博客就有,不过我也再放一份,ok

4.UserageException自定义异常

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1package cn.dawn.day17selfexceptionresolver.userexception;
2
3/**
4 * Created by Dawn on 2018/3/30.
5 */
6public class UserageException extends Exception {
7    public UserageException() {
8        super();
9    }
10
11    public UserageException(String message) {
12        super(message);
13    }
14}
15

 

5.UsernameException自定义异常

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1package cn.dawn.day17selfexceptionresolver.userexception;
2
3/**
4 * Created by Dawn on 2018/3/30.
5 */
6public class UsernameException extends Exception {
7    public UsernameException() {
8        super();
9    }
10
11    public UsernameException(String message) {
12        super(message);
13    }
14}
15

 

6.自定义的处理器和处理方法

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1package cn.dawn.day17selfexceptionresolver;
2
3import cn.dawn.day17selfexceptionresolver.userexception.UserageException;
4import cn.dawn.day17selfexceptionresolver.userexception.UsernameException;
5import org.springframework.stereotype.Controller;
6import org.springframework.web.bind.annotation.RequestMapping;
7
8/**
9 * Created by Dawn on 2018/3/28.
10 */
11@Controller
12public class ZDYExceptionController {
13    @RequestMapping("/zidingyiException")
14    public String zidingyiException(String username,Integer userage) throws Exception {
15        if(!username.equals("admin")){
16            throw new UsernameException("登陆名不正确");
17        }
18        if(userage<18){
19            throw new UserageException("未成年,走开");
20        }
21        return "success";
22    }
23}
24

 

7.jsp页面

7.1login.jsp

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>登录</h2>
8<form action="${pageContext.request.contextPath}/zidingyiException" method="post">
9
10    用户名:<input name="username">
11    年龄:<input name="userage">
12
13    <input type="submit" value="登录"/>
14</form>
15</body>
16</html>
17

 

7.2success.jsp

 


1
2
3
4
5
6
7
8
1<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
2<html>
3<body>
4<%--<img src="image/1.jpg">--%>
5<h2>Success!</h2>
6</body>
7</html>
8

 

7.3error.jsp

 


1
2
3
4
5
6
7
8
9
10
11
12
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>ERROR</h2>
8<p>${ex.message}</p>
9服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
10</body>
11</html>
12

 

7.4age.jsp

 


1
2
3
4
5
6
7
8
9
10
11
12
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>年龄错误ERROR</h2>
8<p>${ex.message}</p>
9服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
10</body>
11</html>
12

 

7.5name.jsp

 


1
2
3
4
5
6
7
8
9
10
11
12
1<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
2<html>
3<head>
4    <title>Title</title>
5</head>
6<body>
7<h2>名字错误ERROR</h2>
8<p>${ex.message}</p>
9服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天
10</body>
11</html>
12

 

 

 

8.启动tomcat,访问login.jsp

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++迭代器

2022-1-11 12:36:11

安全技术

tmux常用命令

2021-8-18 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索