Springmvc异常处理器及拦截器实现代码

 更新时间:2020年10月09日 09:31:51   作者:一路繁花似锦绣前程  
这篇文章主要介绍了Springmvc异常处理器及拦截器实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、异常处理器

1、实现HandlerExceptionResolver接口

package com.wuxi.exceptions;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestExceptionResolver implements HandlerExceptionResolver {
  @Override
  public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    ModelAndView mv = new ModelAndView();
    mv.addObject("errorMsg", e.getMessage());//错误信息
    mv.setViewName("error");//请求转发的页面
    return mv;
  }
}

2、springmvc的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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--扫描组件-->
  <context:component-scan base-package="com.wuxi"></context:component-scan>
  <!--视图解析器-->
  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
  <!--参数类型装换器-->
  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="com.wuxi.utils.StringToDateConverter"></bean>
      </set>
    </property>
  </bean>
  <!--配置调度器不拦截静态资源-->
  <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
  <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
  <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
  <!--配置文件解析器对象-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10485760"></property>
  </bean>
  <!--异常处理器-->
  <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean>
  <!--开启springmvc框架注解的支持-->
  <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>

二、拦截器

1、实现HandlerInterceptor接口

package com.wuxi.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ControllerInterceptor implements HandlerInterceptor {
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    System.out.println("controller的方法执行之前执行");
    return true;//true:放行;false:拦截
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    System.out.println("controller的方法执行之后执行");
  }

  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    System.out.println("jsp执行之后执行");
  }
}

2、springmvc的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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--扫描组件-->
  <context:component-scan base-package="com.wuxi"></context:component-scan>
  <!--视图解析器-->
  <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
  <!--参数类型装换器-->
  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="com.wuxi.utils.StringToDateConverter"></bean>
      </set>
    </property>
  </bean>
  <!--配置调度器不拦截静态资源-->
  <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
  <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
  <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
  <!--配置文件解析器对象-->
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10485760"></property>
  </bean>
  <!--异常处理器-->
  <bean id="requestExceptionResolver" class="com.wuxi.exceptions.RequestExceptionResolver"></bean>
  <!--拦截器-->
  <mvc:interceptors>
    <!--可配置多个拦截器,执行顺序pre1->pre2->controller->post2->post1->jsp->after2->after1-->
    <mvc:interceptor>
      <!--拦截的资源路径-->
      <mvc:mapping path="/**"/>
      <!--不拦截的资源路径-->
      <!--<mvc:exclude-mapping path="/hello"/>-->
      <bean class="com.wuxi.interceptors.ControllerInterceptor"></bean>
    </mvc:interceptor>
  </mvc:interceptors>
  <!--开启springmvc框架注解的支持-->
  <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
</beans>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring boot中filter类不能注入@Autowired变量问题

    Spring boot中filter类不能注入@Autowired变量问题

    这篇文章主要介绍了Spring boot中filter类不能注入@Autowired变量问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java面向对象的六原则一法则小结

    java面向对象的六原则一法则小结

    本篇文章主要对java面向对象的六原则一法则进行简要说明,具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • Java Scala的隐式转换详解

    Java Scala的隐式转换详解

    隐式转换是在Scala编译器进行类型匹配时,如果找不到合适的类型,那么隐式转换会让编译器在作用范围内自动推导出来合适的类型。本文通过代码示例介绍了Scala的隐式转换,感兴趣的小伙伴可以参考阅读
    2023-04-04
  • java中建立0-10m的消息(字符串)实现方法

    java中建立0-10m的消息(字符串)实现方法

    下面小编就为大家带来一篇java中建立0-10m的消息(字符串)实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • java开发环境的完整搭建过程

    java开发环境的完整搭建过程

    这篇文章主要给大家介绍了关于java开发环境的完整搭建过程,文中介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • SpringBoot2实现MessageQueue消息队列

    SpringBoot2实现MessageQueue消息队列

    本文主要介绍了 SpringBoot2实现MessageQueue消息队列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • java操作elasticsearch详细方法总结

    java操作elasticsearch详细方法总结

    elasticsearch是使用Java编写的一种开源搜索引擎,也是一种分布式的搜索引擎架构,这篇文章主要给大家介绍了关于java操作elasticsearch的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • 一篇文章带你解决 IDEA 每次新建项目 maven home directory 总是改变的问题

    一篇文章带你解决 IDEA 每次新建项目 maven home directory 总是改变的问题

    这篇文章主要介绍了一篇文章带你解决 IDEA 每次新建项目 maven home directory 总是改变的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • Java多线程中断机制三种方法及示例

    Java多线程中断机制三种方法及示例

    这篇文章主要介绍了Java多线程中断机制三种方法及示例,向大家分享了这三种方法的介绍几代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • IntelliJ IDEA2023中运行Spring Boot找不到VM options进行端口的修改的问题解决

    IntelliJ IDEA2023中运行Spring Boot找不到VM options进

    这篇文章主要介绍了IntelliJ IDEA2023中运行Spring Boot找不到VM options进行端口的修改的问题解决,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友可以参考下
    2023-11-11

最新评论