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

简介: ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------     自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在xml配置文件中有个不同的操作,我一会重点列出来   案例开始:     1.

 

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

 

 

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

 

案例开始:

 

  1.自定义异常类:UserageException

 

package cn.dawn.day17selfexceptionresolver.userexception;

/**
 * Created by Dawn on 2018/3/30.
 */
public class UserageException extends Exception {
    public UserageException() {
        super();
    }

    public UserageException(String message) {
        super(message);
    }
}

  2.自定义异常类:UsernameException

 

package cn.dawn.day17selfexceptionresolver.userexception;

/**
 * Created by Dawn on 2018/3/30.
 */
public class UsernameException extends Exception {
    public UsernameException() {
        super();
    }

    public UsernameException(String message) {
        super(message);
    }
}

 

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

 

package cn.dawn.day17selfexceptionresolver;

import cn.dawn.day17selfexceptionresolver.userexception.UserageException;
import cn.dawn.day17selfexceptionresolver.userexception.UsernameException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Dawn on 2018/3/28.
 */
@Controller
public class ZDYExceptionController {
    @RequestMapping("/zidingyiException")
    public String zidingyiException(String username,Integer userage) throws Exception {
        if(!username.equals("admin")){
            throw new UsernameException("登陆名不正确");
        }
        if(userage<18){
            throw new UserageException("未成年,走开");
        }
        return "success";
    }
}

 

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

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

    <!--包扫描器-->
    <context:component-scan base-package="cn.dawn.day16exceptionhigh"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/day16/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <!--默认出现异常跳转的页面-->
        <property name="defaultErrorView" value="error"></property>
        <!--这个可以注入异常对象,就像catch参数里的(Exception ex)一样-->
        <property name="exceptionAttribute" value="ex"></property>

        <!--自定义的异常-->
        <property name="exceptionMappings">
            <props>
                <prop key="cn.dawn.day16exceptionhigh.userexception.UserageException">age</prop>
                <prop key="cn.dawn.day16exceptionhigh.userexception.UsernameException">name</prop>
            </props>
        </property>
    </bean>

</beans>

  上面标红的是重中之重

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

  5.jsp页面

    5.1age.jsp

 

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

 

    5.2name.jsp

 

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

 

    5.3login.jsp

 

<%@ page  pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"  %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登录</h2>
<form action="${pageContext.request.contextPath}/zidingyiException" method="post">

    用户名:<input name="username">
    年龄:<input name="userage">

    <input type="submit" value="登录"/>
</form>
</body>
</html>

 

    5.4success.jsp

 

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

 

    5.5error.jsp

 

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

 

 

 

  6.启动tomcat访问login.jsp

 

目录
相关文章
|
6月前
|
前端开发 Java 编译器
SpringMVC自定义注解---[详细介绍]
SpringMVC自定义注解---[详细介绍]
27 0
|
6月前
|
监控 Java 编译器
SpringMVC之自定义注解
SpringMVC之自定义注解
33 0
|
7月前
|
前端开发 Java
48SpringMVC - 参数绑定(自定义)
48SpringMVC - 参数绑定(自定义)
27 0
|
6月前
|
安全 Java 数据库连接
【springMvc】自定义注解的使用方式
【springMvc】自定义注解的使用方式
64 0
|
5月前
|
前端开发 安全 Java
解锁高级技巧:玩转 Spring MVC 自定义拦截器的神奇世界
解锁高级技巧:玩转 Spring MVC 自定义拦截器的神奇世界
79 0
|
5月前
|
缓存 安全 Java
SpringMVC自定义注解和使用
SpringMVC自定义注解和使用
95 0
|
5月前
|
Java
springmvc之自定义注解-->自定义注解简介,基本案例和aop自定义注解
springmvc之自定义注解-->自定义注解简介,基本案例和aop自定义注解
34 0
|
5月前
|
Java 开发者
SpringMVC----自定义注解
SpringMVC----自定义注解
29 0
|
5月前
|
Java
【SpringMVC】之自定义注解
【SpringMVC】之自定义注解
36 0
|
6月前
|
JSON 安全 Java
SpringMVC之自定义注解(这期博客带你领略自定义注解的魅力)
SpringMVC之自定义注解(这期博客带你领略自定义注解的魅力)
45 0
SpringMVC之自定义注解(这期博客带你领略自定义注解的魅力)