溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Springmvc實(shí)現(xiàn)異常處理器及攔截器

發(fā)布時(shí)間:2020-10-28 17:35:13 來源:億速云 閱讀:112 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)Springmvc實(shí)現(xiàn)異常處理器及攔截器,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

一、異常處理器

1、實(shí)現(xiàn)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());//錯(cuò)誤信息
    mv.setViewName("error");//請(qǐng)求轉(zhuǎn)發(fā)的頁面
    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>
  <!--參數(shù)類型裝換器-->
  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="com.wuxi.utils.StringToDateConverter"></bean>
      </set>
    </property>
  </bean>
  <!--配置調(diào)度器不攔截靜態(tài)資源-->
  <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
  <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
  <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
  <!--配置文件解析器對(duì)象-->
  <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、實(shí)現(xiàn)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的方法執(zhí)行之前執(zhí)行");
    return true;//true:放行;false:攔截
  }

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

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

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>
  <!--參數(shù)類型裝換器-->
  <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
      <set>
        <bean class="com.wuxi.utils.StringToDateConverter"></bean>
      </set>
    </property>
  </bean>
  <!--配置調(diào)度器不攔截靜態(tài)資源-->
  <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
  <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
  <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
  <!--配置文件解析器對(duì)象-->
  <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>
    <!--可配置多個(gè)攔截器,執(zhí)行順序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實(shí)現(xiàn)異常處理器及攔截器,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI