spring—拦截器和异常

SpringMVC的拦截器

SpringMVC拦截器-拦截器的作用

Spring MVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理。

将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(InterceptorChain)。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体实现。

自定义拦截器很简单,只有如下三步:
①创建拦截器类实现HandlerInterceptor接口

public class MyInterceptor1 implements HandlerInterceptor {public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle");return true;}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle");}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println(" afterCompletion");}
}
public class MyInterceptor2 implements HandlerInterceptor {public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle2");return true;}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle2");}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println(" afterCompletion2");}
}

②配置拦截器

    <mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><bean class="com.controller.MyInterceptor1"/></mvc:interceptor><mvc:interceptor><mvc:mapping path="/**"/><bean class="com.controller.MyInterceptor2"/></mvc:interceptor></mvc:interceptors>

③测试拦截器的拦截效果

    @RequestMapping(value = {"/qq2"})public  ModelAndView method2(ModelAndView modelAndView){modelAndView.addObject("name","ccc");modelAndView.setViewName("success");return modelAndView;}

结论:

当拦截器的preHandle方法返回true则会执行目标资源,如果返回false则不执行目标资源

多个拦截器情况下,配置在前的先执行,配置在后的后执行

拦截器中的方法执行顺序是:preHandler-------目标资源----postHandle---- afterCompletion

SpringMVC异常处理机制

异常处理的思路

系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试等手段减少运行时异常的发生。

系统的Dao、Service、Controller出现都通过throws Exception向上抛出,最后由SpringMVC前端控制器交由异常处理器进行异常处理,

异常处理两种方式

① 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="defaultErrorView" value="error"/><property name="exceptionMappings"><map><entry key="java.lang.NumberFormatException" value="error"/></map></property></bean>
    @RequestMapping(value = {"/qq2"})public  ModelAndView method2(ModelAndView modelAndView) throws Exception{String s="abc";System.out.println(Integer.parseInt(s));modelAndView.addObject("name","ccc");modelAndView.setViewName("success");return modelAndView;}

② 实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器

自定义异常处理步骤

①创建异常处理器类实现HandlerExceptionResolver

public class MyExceptionResolver implements HandlerExceptionResolver {public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {ModelAndView modelAndView = new ModelAndView();modelAndView.setViewName("error");return modelAndView;}
}

②配置异常处理器

    <bean class="com.controller.MyExceptionResolver"/>

③编写异常页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>error</h1>
</body>
</html>

④测试异常跳转

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391873.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

29/07/2010 sunrise

** .. We can only appreciate the miracle of a sunrise if we have waited in the darkness .. 人们在黑暗中等待着&#xff0c;那是期盼着如同日出般的神迹出现 .. 附&#xff1a;27/07/2010 sunrise ** --- 31 July 改动转载于:https://www.cnblogs.com/orderedchaos/archi…

密度聚类dbscan_DBSCAN —基于密度的聚类方法的演练

密度聚类dbscanThe idea of having newer algorithms come into the picture doesn’t make the older ones ‘completely redundant’. British statistician, George E. P. Box had once quoted that, “All models are wrong, but some are useful”, meaning that no model…

node aws 内存溢出_在AWS Elastic Beanstalk上运行生产Node应用程序的现实

node aws 内存溢出by Jared Nutt贾里德努特(Jared Nutt) 在AWS Elastic Beanstalk上运行生产Node应用程序的现实 (The reality of running a production Node app on AWS Elastic Beanstalk) 从在AWS的ELB平台上运行生产Node应用程序两年的经验教训 (Lessons learned from 2 y…

Day2-数据类型

数据类型与内置方法 数据类型 数字字符串列表字典元组集合字符串 1.用途 用来描述某个物体的特征&#xff1a;姓名&#xff0c;性别&#xff0c;爱好等 2.定义方式 变量名 字符串 如&#xff1a;name huazai 3.常用操作和内置方法 1.按索引取值&#xff1a;&#xff08;只能取…

嵌套路由

父组件不能用精准匹配&#xff0c;否则只组件路由无法展示 转载于:https://www.cnblogs.com/dianzan/p/11308146.html

leetcode 992. K 个不同整数的子数组(滑动窗口)

给定一个正整数数组 A&#xff0c;如果 A 的某个子数组中不同整数的个数恰好为 K&#xff0c;则称 A 的这个连续、不一定独立的子数组为好子数组。 &#xff08;例如&#xff0c;[1,2,3,1,2] 中有 3 个不同的整数&#xff1a;1&#xff0c;2&#xff0c;以及 3。&#xff09; …

从完整的新手到通过TensorFlow开发人员证书考试

I recently graduated with a bachelor’s degree in Civil Engineering and was all set to start with a Master’s degree in Transportation Engineering this fall. Unfortunately, my plans got pushed to the winter term because of COVID-19. So as of January this y…

微信开发者平台如何编写代码_编写超级清晰易读的代码的初级开发者指南

微信开发者平台如何编写代码Writing code is one thing, but writing clean, readable code is another thing. But what is “clean code?” I’ve created this short clean code for beginners guide to help you on your way to mastering and understanding the art of c…

【转】PHP面试题总结

PHP面试总结 PHP基础1&#xff1a;变量的传值与引用。 2&#xff1a;变量的类型转换和判断类型方法。 3&#xff1a;php运算符优先级&#xff0c;一般是写出运算符的运算结果。 4&#xff1a;PHP中函数传参&#xff0c;闭包&#xff0c;判断输出的echo&#xff0c;print是不是函…

Winform控件WebBrowser与JS脚本交互

1&#xff09;在c#中调用js函数 如果要传值&#xff0c;则可以定义object[]数组。 具体方法如下例子&#xff1a; 首先在js中定义被c#调用的方法: function Messageaa(message) { alert(message); } 在c#调用js方法Messageaa private void button1_Click(object …

从零开始撸一个Kotlin Demo

####前言 自从google将kotlin作为亲儿子后就想用它撸一管app玩玩&#xff0c;由于工作原因一直没时间下手&#xff0c;直到项目上线后才有了空余时间&#xff0c;期间又由于各种各样烦人的事断了一个月&#xff0c;现在终于开发完成项目分为服务器和客户端&#xff1b;服务器用…

移动平均线ma分析_使用动态移动平均线构建交互式库存量和价格分析图

移动平均线ma分析I decided to code out my own stock tracking chart despite a wide array of freely available tools that serve the same purpose. Why? Knowledge gain, it’s fun, and because I recognize that a simple project can generate many new ideas. Even t…

敏捷开发创始人_开发人员和技术创始人如何将他们的想法转化为UI设计

敏捷开发创始人by Simon McCade西蒙麦卡德(Simon McCade) 开发人员和技术创始人如何将他们的想法转化为UI设计 (How developers and tech founders can turn their ideas into UI design) Discover how to turn a great idea for a product or service into a beautiful UI de…

在ubuntu怎样修改默认的编码格式

ubuntu修改系统默认编码的方法是&#xff1a;1. 参考 /usr/share/i18n/SUPPORTED 编辑/var/lib/locales/supported.d/* gedit /var/lib/locales/supported.d/localgedit /var/lib/locales/supported.d/zh-hans如&#xff1a;more /var/lib/locales/supported.d/localzh_CN GB18…

JAVA中PO,BO,VO,DTO,POJO,Entity

https://my.oschina.net/liaodo/blog/2988512转载于:https://www.cnblogs.com/dianzan/p/11311217.html

【Lolttery】项目开发日志 (三)维护好一个项目好难

项目的各种配置开始出现混乱的现象了 在只有一个人开发的情况下也开始感受到维护一个项目的难度。 之前明明还好用的东西&#xff0c;转眼就各种莫名其妙的报错&#xff0c;完全不知道为什么。 今天一天的工作基本上就是整理各种配置。 再加上之前数据库设计出现了问题&#xf…

leetcode 567. 字符串的排列(滑动窗口)

给定两个字符串 s1 和 s2&#xff0c;写一个函数来判断 s2 是否包含 s1 的排列。 换句话说&#xff0c;第一个字符串的排列之一是第二个字符串的子串。 示例1: 输入: s1 “ab” s2 “eidbaooo” 输出: True 解释: s2 包含 s1 的排列之一 (“ba”). 解题思路 和s1每个字符…

静态变数和非静态变数_统计资料:了解变数

静态变数和非静态变数Statistics 101: Understanding the different type of variables.统计101&#xff1a;了解变量的不同类型。 As we enter the latter part of the year 2020, it is safe to say that companies utilize data to assist in making business decisions. F…

代码走查和代码审查_如何避免代码审查陷阱降低生产率

代码走查和代码审查Code reviewing is an engineering practice used by many high performing teams. And even though this software practice has many advantages, teams doing code reviews also encounter quite a few code review pitfalls.代码审查是许多高性能团队使用…

Zabbix3.2安装

一、环境 OS: CentOS7.0.1406 Zabbix版本: Zabbix-3.2 下载地址: http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm MySQL版本: 5.6.37 MySQL: http://repo.mysql.com/mysql-community-release-el7-5.noarch.r…