SSM-SpringMVC-24:SpringMVC异常高级之自定义异常

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

 

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

 

 

自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在xml配置文件中有个不同的操作,我一会重点列出来

 

案例开始:

 

1.自定义异常类: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

2.自定义异常类: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

 

3.定义处理器和处理方法:

 


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

 

在自己的xml大配置中:SimpleMappingExceptionResolver配一个exceptionMappings

 


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
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.day16exceptionhigh"></context:component-scan>
15    <!--视图解析器-->
16    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17        <property name="prefix" value="/day16/"></property>
18        <property name="suffix" value=".jsp"></property>
19    </bean>
20
21    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
22        <!--默认出现异常跳转的页面-->
23        <property name="defaultErrorView" value="error"></property>
24        <!--这个可以注入异常对象,就像catch参数里的(Exception ex)一样-->
25        <property name="exceptionAttribute" value="ex"></property>
26
27        <!--自定义的异常-->
28        <property name="exceptionMappings">
29            <props>
30                <prop key="cn.dawn.day16exceptionhigh.userexception.UserageException">age</prop>
31                <prop key="cn.dawn.day16exceptionhigh.userexception.UsernameException">name</prop>
32            </props>
33        </property>
34    </bean>
35
36</beans>
37

上面标红的是重中之重

4.将web.xml的中央调度器上下文配置位置改为上面新的那个xml

5.jsp页面

5.1age.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

 

5.2name.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

 

5.3login.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

 

5.4success.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

 

5.5error.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

 

 

 

6.启动tomcat访问login.jsp

 

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

C++异常

2022-1-11 12:36:11

安全经验

最新 Mac 恶意软件现身,安全专家提醒不要过于自信

2017-6-14 11:12:22

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