SSM-SpringMVC-24:SpringMVC异常高级之自定义异常
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在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异常高级之自定义异常的更多相关文章
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- 基于SpringMVC的全局异常处理器介绍(转)
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- 13.SpringMVC之全局异常
我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发.测试通过手段减少运行时异常的发生.在开发中,不管是dao层 ...
- springmvc(五) 数据回显与自定义异常处理器
这章讲解一下springmvc的数据回显和自定义异常处理器的使用,两个都很简单 --WH 一.数据回显技术 Springmvc默认支持对pojo类型的数据回显,默认不支持简单类型的数据回显 1.1.什 ...
- SSM,即Spring+SpringMVC+MyBatis三个开源框架的整合框架集。
SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...
- 关于SpringMVC的全局异常处理器
近几天又温习了一下SpringMVC的运行机制以及原理 我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)层,常用的注解有@Controller.@RequestM ...
- Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)
[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/de ...
- (转)SpringMVC学习(八)——SpringMVC中的异常处理器
http://blog.csdn.net/yerenyuan_pku/article/details/72511891 SpringMVC在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常 ...
- 简单易学的SSM(Spring+SpringMVC+MyBatis)整合
SSM(Spring+SpringMVC+MyBatis)的整合: 具体执行过程:1.用户在页面向后台发送一个请求 2.请求由DispatcherServlet 前端控制器拦截交给SpringMVC管 ...
随机推荐
- LDA主体模型
一)LDA作用 传统判断两个文档相似性的方法是通过查看两个文档共同出现的单词的多少,如TF-IDF等,这种方法没有考虑到文字背后的语义关联,可能在两个文档共同出现的单词很少甚至没有,但两个文档是相似的 ...
- centos 下安装mysql-5.6.11
这次是在centos6.4下安装mysql,在安装之前,你要先确定你的linux已经安装了这些包: wget, gcc-c++, ncurses-devel ,cmake, make ,perl 如果 ...
- Java的依赖注入(控制反转)
两个主角"依赖注入"和"控制反转": 1.二都说的都是同一件事,只是叫法不同.是一个重要的面向对象编程的法则,也是一种设计模式: 2.英文原称:依赖注入,Dep ...
- 使用swift语言进行IOS应用开发
在Swift中能够直接使用Objective-C语言提供的api (包括系统框架与自己的定制代码),也能够在Objective-C中使用Swift提供的类和api ,还能够在一个工程中同时混合使用Sw ...
- 【43】Activity的几种LaunchMode及使用场景
standard 模式 这是默认模式,每次激活Activity时都会创建Activity实例,并放入任务栈中.使用场景:大多数Activity. singleTop 模式 如果在任务的栈顶正好存在该A ...
- SharePoint 2010 -- .Net托管客户端模型简单示例
.Net托管客户端模型,是SharePoint2010推出的三种客户端模型".NET托管"."ECMAScript"."Sliverlight&quo ...
- 属性动画基础之ValueAnimator
概述 属性动画是谷歌在android3.0(API level 11)时候给我们带来了属性动画,真正意义上带来了"动画",以前的帧动画也就4中效果的组合(旋转.淡入淡出.放大缩小. ...
- leetcode(58)-Range Sum Query - Immutable
题目: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclu ...
- C# /VB.NET 插入、修改、删除Word批注
批注内容可以是对某段文字或内容的注释,也可以是对文段中心思想的概括提要,或者是对文章内容的评判.疑问,以及在阅读时给自己或他人起到提示作用.本篇文章中将介绍如何在C#中操作Word批注,主要包含以下要 ...
- Android Selector原理
android的selector对于android开发者而言再熟悉不过了,只要定义一个drawable目录下定义一个selector的xml文件,在布局文件中引用这个xml文件或者在代码中setBac ...