SSM-SpringMVC-25:SpringMVC异常顶级之自定义异常解析器
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
上篇博客相信大家也看到了,自定义异常,用了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异常顶级之自定义异常解析器的更多相关文章
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...
- 7.SpringMVC 配置式开发-ModelAndView和视图解析器
ModelAndView 1.Model(模型) 1.model的本质就是HashMap,向模型中添加数据,就是往HashMap中去添加数据 2.HashMap 是一个单向查找数组,单向链表数组 3. ...
- 【SpringMVC】SpringMVC系列10之视图与视图解析器
10.视图与视图解析器 10.1.概述 请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...
- SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传
1.视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象 对于那些返回 String,View 或 ModeMap 等类型的处理方法,SpringMVC 也会在内部将 ...
- SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器
mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...
- SpringMVC自动封装List对象 —— 自定义参数解析器
前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...
- springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)
HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...
- springmvc配置式开发下的视图解析器
多个视图解析器优先级:
- springmvc 源码分析(三) -- 自定义处理器映射器和自定义处理器适配器,以及自定义参数解析器 和错误跳转自定页面
测试环境搭建: 本次搭建是基于springboot来实现的,代码在码云的链接:https://gitee.com/yangxioahui/thymeleaf.git DispatcherServlet ...
随机推荐
- php opcode缓存
本文移至:http://www.phpgay.com/Article/detail/classid/2/id/61.html 1.什么是opcode 解释器分析代码之后,生成可以直接运行的中间代码,就 ...
- 【Matlab编程】matlab 画图
1. 不用截图工具就可以将图保存成图像格式,并且没有背景颜色:saveas(gcf ,'outputname','png/jpg'),第三项省略时默认为fig.m文件 2. 计算形如(-1)^2/ ...
- 数据库用户映射到SQL Server登录名
由于不是固定在一如电脑开始,经常需要把数据备份与恢复.或者是帮助客户修改程序,接收到客户备份好的数据,需要在电脑恢复. 这样就需要将数据库用户映射到 SQL Server 登录名.如何映射?可使用下面 ...
- Java进阶(十五)Java中设置session的详细解释
Java中设置session的详细解释 简单通俗的讲session就是象一个临时的容器,用来存放临时的东西.从你登陆开始就保存在session里,当然你可以自己设置它的有效时间和页面,举个简单的例子: ...
- android 热修补之andfix实践
首先有这方面需要的请到https://github.com/xiangzhihong/andfixDemo/tree/master下载例子 对于网上提供的热补丁修复就不多说了,主要有这3种方式可以实现 ...
- rails小重构:将图片加入产品Model
原先的产品product模式中存放的是图片的url,必须手动将图片存入指定目录中.现在略作改动,在数据库中新建一个pictures表,其设定如下: class CreatePictures < ...
- ELF 动态链接 - so 的 .dynamic 段
动态链接文件中最重要的段就是 .dynamic段 这个段里保存了动态链接器需要的最基本的信息 比如:1. 依赖于哪些共享对象, d_tag = DT_NEED, d_ptr 表示共享对象文件名 2 ...
- getElementById 用法的一个技巧
假设实现把 TextBox1 的字符实时的拷贝到 TextBox2 中,代码如下: <Script language="Javascript"> fun ...
- eclipse中tomcat内存溢出设置
Eclipse里启动Tomcat,配置内存大小 2009年12月11日 星期五 10:50 一般安装完eclipse之后,在安装目录下你应该可以看到有一个 eclipse.ini 文件,对了,就是在这 ...
- java安全——BASE64
这个主题主要是关于java安全的,应该来说算是个大杂烩吧,但是又不缺乏实用性,算是作为一个总结,用的时候可以作为参考. 1.使用BASE64加解密 在java加密技术中,BASE64算是一种最简单.最 ...