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

上篇博客相信大家也看到了,自定义异常,用了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<){
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="data: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

SSM-SpringMVC-25:SpringMVC异常顶级之自定义异常解析器的更多相关文章

  1. springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)

    在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...

  2. 7.SpringMVC 配置式开发-ModelAndView和视图解析器

    ModelAndView 1.Model(模型) 1.model的本质就是HashMap,向模型中添加数据,就是往HashMap中去添加数据 2.HashMap 是一个单向查找数组,单向链表数组 3. ...

  3. 【SpringMVC】SpringMVC系列10之视图与视图解析器

    10.视图与视图解析器 10.1.概述     请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...

  4. SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传

    1.视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象 对于那些返回 String,View 或 ModeMap 等类型的处理方法,SpringMVC 也会在内部将 ...

  5. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  6. SpringMVC自动封装List对象 —— 自定义参数解析器

    前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...

  7. springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)

    HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...

  8. springmvc配置式开发下的视图解析器

    多个视图解析器优先级:

  9. springmvc 源码分析(三) -- 自定义处理器映射器和自定义处理器适配器,以及自定义参数解析器 和错误跳转自定页面

    测试环境搭建: 本次搭建是基于springboot来实现的,代码在码云的链接:https://gitee.com/yangxioahui/thymeleaf.git DispatcherServlet ...

随机推荐

  1. 校招:Vobile阜博通2015校园招聘

    关于Vobile阜博通校招(10-11月份),耗时将近一个月,现整理分享给大家. 1 浙大笔试无选择填空,问答题为主,偏语言的个人理解,不在意具体语言方向(C/C++/Java).(1)描述C.C++ ...

  2. C++之虚函数

    <span style="font-size:18px;">#include <iostream> using namespace std ; class ...

  3. LeetCode(46)-Remove Nth Node From End of List

    题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  4. 杭电ACM 1002题

    import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(S ...

  5. $cordovaCamera 插件 上传头像 图片功能

    首先要注入  $cordovaCamera 使用相机拍照 var useCamera = function() { var options = { //这些参数可能要配合着使用,比如选择了source ...

  6. java集合框架--List、Set、Map

      1.List:有序的 collection(也称为序列).此接口可以对列表中每个元素的插入位置进行精确地控制.可以根据元素的在列表中的位置访问元素,并搜索列表中的元素.列表允许重复的元素.    ...

  7. 乐学习知选择--我的J2EE技术历程

    转眼换工作已经两个多月了,转眼今年已经到9月份了,转眼女朋友也来到了自己身边.有太多的转眼,如今在这个经理不在的早晨,可以肆意的点点这里看看那里,想想自己,有点吉利思的感觉. 这两个多月,知道了什么叫 ...

  8. 前端技术之_CSS详解第五天

    前端技术之_CSS详解第五天 一.行高和字号 1.1 行高 CSS中,所有的行,都有行高.盒模型的padding,绝对不是直接作用在文字上的,而是作用在“行”上的. <!DOCTYPE html ...

  9. Linux常用资源(不断改进中)

    Linux常用命令全集:  http://linux.chinaitlab.com/special/linuxcom/Index.html ubuntu 12.04 配置指南: http://www. ...

  10. nginx flv点播服务器搭建

    首先选用Nginx+Nginx-rtmp-module作为点播服务器,安装文章:https://www.atlantic.NET/community/howto/install-rtmp-ubuntu ...