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

简介:   ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------     上篇博客相信大家也看到了,自定义异常,用了SimpleMappingExceptionResolver这个解析器,本次要讲的是自定义异常解析器: 自己定义的异常解析器,实现了Hand...

 

 

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

 

 

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

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

 

案例

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

 

package cn.dawn.day17selfexceptionresolver.resolver;

import cn.dawn.day17selfexceptionresolver.userexception.UserageException;
import cn.dawn.day17selfexceptionresolver.userexception.UsernameException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Dawn on 2018/3/30.
 */
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView=new ModelAndView();
        /*返回的异常对象*/
        modelAndView.addObject("ex",ex);
        /*判断去那个页面*/
        if(ex instanceof UsernameException){
            modelAndView.setViewName("name");
        }
        if(ex instanceof UserageException){
            modelAndView.setViewName("age");
        }

        return modelAndView;
    }
}

 

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

 

<?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.day17selfexceptionresolver"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/day17/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--自己的异常解析器-->
    <bean class="cn.dawn.day17selfexceptionresolver.resolver.MyHandlerExceptionResolver"></bean>

</beans>

 

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

 

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

  4.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);
    }
}

 

  5.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);
    }
}

 

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

 

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";
    }
}

 

  7.jsp页面

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

 

    7.2success.jsp

 

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

 

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

 

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

 

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

 

 

 

  8.启动tomcat,访问login.jsp

 

目录
相关文章
|
14天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
47 1
|
19天前
|
XML 存储 Java
Spring重要类解析
Spring重要类解析
19 0
|
1月前
|
SQL Java 数据库连接
|
2月前
|
XML Java 数据格式
Spring 应用上下文探秘:生命周期解析与最佳实践
Spring 应用上下文探秘:生命周期解析与最佳实践
73 0
|
1月前
|
消息中间件 Cloud Native Java
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
|
2月前
|
Java 关系型数据库 数据库连接
Spring源码解析--深入Spring事务原理
本文将带领大家领略Spring事务的风采,Spring事务是我们在日常开发中经常会遇到的,也是各种大小面试中的高频题,希望通过本文,能让大家对Spring事务有个深入的了解,无论开发还是面试,都不会让Spring事务成为拦路虎。
33 1
|
18天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
40 1
|
1月前
|
XML Java 开发者
深入解析 Spring 和 Spring Boot 的区别
深入解析 Spring 和 Spring Boot 的区别
|
4天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
14 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
|
4天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
22 2
从入门到精通:Spring基础注解的全面解析

推荐镜像

更多