温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-28 17:35:13 来源:亿速云 阅读:114 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关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配置文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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配置文件

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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>

以上就是Springmvc实现异常处理器及拦截器,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI