最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年10月09日 09:31:51   作者:一路繁花似錦繡前程  
這篇文章主要介紹了Springmvc異常處理器及攔截器實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、異常處理器

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ā)的頁(yè)面
    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>
  <!--參數(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配置文件

<?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>
  <!--參數(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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring boot中filter類不能注入@Autowired變量問題

    Spring boot中filter類不能注入@Autowired變量問題

    這篇文章主要介紹了Spring boot中filter類不能注入@Autowired變量問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java面向?qū)ο蟮牧瓌t一法則小結(jié)

    java面向?qū)ο蟮牧瓌t一法則小結(jié)

    本篇文章主要對(duì)java面向?qū)ο蟮牧瓌t一法則進(jìn)行簡(jiǎn)要說明,具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • Java Scala的隱式轉(zhuǎn)換詳解

    Java Scala的隱式轉(zhuǎn)換詳解

    隱式轉(zhuǎn)換是在Scala編譯器進(jìn)行類型匹配時(shí),如果找不到合適的類型,那么隱式轉(zhuǎn)換會(huì)讓編譯器在作用范圍內(nèi)自動(dòng)推導(dǎo)出來合適的類型。本文通過代碼示例介紹了Scala的隱式轉(zhuǎn)換,感興趣的小伙伴可以參考閱讀
    2023-04-04
  • java中建立0-10m的消息(字符串)實(shí)現(xiàn)方法

    java中建立0-10m的消息(字符串)實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄猨ava中建立0-10m的消息(字符串)實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • java開發(fā)環(huán)境的完整搭建過程

    java開發(fā)環(huán)境的完整搭建過程

    這篇文章主要給大家介紹了關(guān)于java開發(fā)環(huán)境的完整搭建過程,文中介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • SpringBoot2實(shí)現(xiàn)MessageQueue消息隊(duì)列

    SpringBoot2實(shí)現(xiàn)MessageQueue消息隊(duì)列

    本文主要介紹了 SpringBoot2實(shí)現(xiàn)MessageQueue消息隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • java操作elasticsearch詳細(xì)方法總結(jié)

    java操作elasticsearch詳細(xì)方法總結(jié)

    elasticsearch是使用Java編寫的一種開源搜索引擎,也是一種分布式的搜索引擎架構(gòu),這篇文章主要給大家介紹了關(guān)于java操作elasticsearch的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • 一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問題

    一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問題

    這篇文章主要介紹了一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java多線程中斷機(jī)制三種方法及示例

    Java多線程中斷機(jī)制三種方法及示例

    這篇文章主要介紹了Java多線程中斷機(jī)制三種方法及示例,向大家分享了這三種方法的介紹幾代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • IntelliJ IDEA2023中運(yùn)行Spring Boot找不到VM options進(jìn)行端口的修改的問題解決

    IntelliJ IDEA2023中運(yùn)行Spring Boot找不到VM options進(jìn)

    這篇文章主要介紹了IntelliJ IDEA2023中運(yùn)行Spring Boot找不到VM options進(jìn)行端口的修改的問題解決,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11

最新評(píng)論

焦作市| 横峰县| 哈巴河县| 通化县| 库车县| 左贡县| 太谷县| 汕尾市| 东方市| 饶河县| 吉安市| 高唐县| 阜康市| 个旧市| 独山县| 贺州市| 泊头市| 曲麻莱县| 金昌市| 清水县| 二连浩特市| 无棣县| 开远市| 灵宝市| 芜湖县| 永年县| 富源县| 朝阳区| 色达县| 南澳县| 甘南县| 米易县| 松江区| 竹北市| 保靖县| 花莲市| 甘德县| 喜德县| 黄陵县| 新宾| 林甸县|