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

自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在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<){
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="data: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

SSM-SpringMVC-24:SpringMVC异常高级之自定义异常的更多相关文章

  1. Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...

  2. 基于SpringMVC的全局异常处理器介绍(转)

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  3. 13.SpringMVC之全局异常

    我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生.在开发中,不管是dao层 ...

  4. springmvc(五) 数据回显与自定义异常处理器

    这章讲解一下springmvc的数据回显和自定义异常处理器的使用,两个都很简单 --WH 一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什 ...

  5. SSM,即Spring+SpringMVC+MyBatis三个开源框架的整合框架集。

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...

  6. 关于SpringMVC的全局异常处理器

    近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...

  7. Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

    [JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...

  8. (转)SpringMVC学习(八)——SpringMVC中的异常处理器

    http://blog.csdn.net/yerenyuan_pku/article/details/72511891 SpringMVC在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常 ...

  9. 简单易学的SSM(Spring+SpringMVC+MyBatis)整合

    SSM(Spring+SpringMVC+MyBatis)的整合: 具体执行过程:1.用户在页面向后台发送一个请求 2.请求由DispatcherServlet 前端控制器拦截交给SpringMVC管 ...

随机推荐

  1. 写论文如何做相关工作(realted work)的调研

    1.找一篇目标研究领域的中文综述,读懂,对该领域有了些基本的了解,如何找到好的综述,就是要关注一些大牛的实验组的综述和进展: 2.找该中文综述引用的外文文献来看,通常是一些比较经典的文献 3.找这些外 ...

  2. Java 继承Thread类和实现Runnable接口的区别

    ava中线程的创建有两种方式: 1.  通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2.  通过实现Runnable接口,实例化Thread类 在实际应用中,我 ...

  3. android自定义状态栏颜色

    我们知道IOS上的应用,状态栏的颜色总能与应用标题栏颜色保持一致,用户体验很不错,那安卓是否可以呢?若是在安卓4.4之前,答案是否定的,但在4.4之后,谷歌允许开发者自定义状态栏背景颜色啦,这是个不错 ...

  4. Linux - 工作管理(job control),jobs,fg,bg,kill

    什么是工作管理? 『进行工作管理的行为中, 其实每个工作都是目前 bash 的子程序,亦即彼此之间是有相关性的. 我们无法以 job control 的方式由 tty1 的环境去管理 tty2 的 b ...

  5. HBase缓存的使用

    hbase中的缓存分了两层:memstore和blockcache. 其中memstore供写使用,写请求会先写入memstore,regionserver会给每个region提供一个memstore ...

  6. Java I/O最简单的几个类

    今天把I/O中最简单的几个类整理了一下,之所以整理最简单的,是因为这样会让我更加快速方便的理顺这里面的东西,以前每一次用的时候都要先百度一下,觉得很烦. 首先需要先看一下Read,Write和Stre ...

  7. 面试之路(6)-BAT面试之操作系统内存详解

    本文主要参考两篇博客,读后整理出来,以供大家阅读,链接如下: http://blog.jobbole.com/95499/?hmsr=toutiao.io&utm_medium=toutiao ...

  8. LeetCode之旅(21)-Swap Nodes in Pairs

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  9. LeetCode(62)-Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  10. OS X10.10下HomeBrew的安装提示

    apple@kissAir: c_src$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...