SSM-SpringMVC-26:SpringMVC异常骇级之自定义异常注解版

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

 

 

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

 

 

注解的方法实现异常解析,话不多说,直接搞起,和以前一样的习惯,和上篇博客一样的代码放后面,不一样的在前面

 

案例:

1.自定义处理器和处理方法

 


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
1package cn.dawn.day18annotationexception;
2
3import cn.dawn.day18annotationexception.userexception.UserageException;
4import cn.dawn.day18annotationexception.userexception.UsernameException;
5import org.springframework.stereotype.Controller;
6import org.springframework.web.bind.annotation.ExceptionHandler;
7import org.springframework.web.bind.annotation.RequestMapping;
8import org.springframework.web.servlet.ModelAndView;
9
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12
13/**
14 * Created by Dawn on 2018/3/28.
15 */
16@Controller
17public class ZDYExceptionController {
18
19
20    /*作用于这俩个*/
21    @ExceptionHandler({UsernameException.class,UserageException.class})
22    public ModelAndView resolveException(Exception ex) {
23        ModelAndView modelAndView=new ModelAndView();
24        /*返回的异常对象*/
25        modelAndView.addObject("ex",ex);
26        /*判断去那个页面*/
27        if(ex instanceof UsernameException){
28            modelAndView.setViewName("name");
29        }
30        if(ex instanceof UserageException){
31            modelAndView.setViewName("age");
32        }
33
34        return modelAndView;
35    }
36
37
38
39
40    @RequestMapping("/annotationException")
41    public String annotationException(String username,Integer userage) throws Exception {
42        if(!username.equals("admin")){
43            throw new UsernameException("登陆名不正确");
44        }
45        if(userage<18){
46            throw new UserageException("未成年,走开");
47        }
48        return "success";
49    }
50}
51

 

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
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.day18annotationexception"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day18/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21
22</beans>
23

 

3.修改web.xml的中央调度器的上下文配置路径

4.login.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}/annotationException" method="post">
9
10    用户名:<input name="username">
11    年龄:<input name="userage">
12
13    <input type="submit" value="登录"/>
14</form>
15</body>
16</html>
17

 

 

 

下面的内容与上篇博客相同


**
5.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

 

 

 

**
6.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

 

 

 

7.jsp页面

 

7.1success.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.2error.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.3age.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.4name.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

安全运维

Redis - 配置认证密码

2021-12-11 11:36:11

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