激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 淺談springMVC攔截器和過濾器總結(jié)

淺談springMVC攔截器和過濾器總結(jié)

2020-07-29 14:24龔細(xì)軍 Java教程

本篇文章主要介紹了springMVC攔截器和過濾器總結(jié),可以用來對(duì)訪問的url進(jìn)行攔截處理,有興趣的可以了解一下。

攔截器: 用來對(duì)訪問的url進(jìn)行攔截處理

用處: 權(quán)限驗(yàn)證,亂碼設(shè)置等

spring-mvc.xml文件中的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/tx
            ">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!--編寫攔截器-->
<mvc:interceptors>
  <!--對(duì)特定的url攔截-->
  <mvc:interceptor>
    <mvc:mapping path="/test.do"/>
    <bean class="com.hbut.interceptor.TestInterceptor"/>
  </mvc:interceptor>
  <mvc:interceptor>
    <!--對(duì)特定的模塊攔截第一級(jí)別攔截-->
    <mvc:mapping path="/test/×/"/>
    <bean class="com.hbut.interceptor.TestInterceptor1"/>
  </mvc:interceptor>
 
  <mvc:interceptor>
    <!--對(duì)特定的模塊攔截-->
    <mvc:mapping path="/test/×"/>
    <bean class="com.hbut.interceptor.TestInterceptor2"/>
  </mvc:interceptor>
 
</mvc:interceptors>

對(duì)所有的url進(jìn)行攔截

?
1
2
3
<mvc:interceptors>
  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>

java代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.hbut.interceptor;
 
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * @Author XiJun.Gong
 * @DATE 2016/6/1.
 * aim:  com.hbut.interceptor
 */
public class TestInterceptor implements HandlerInterceptor {
 
  @Override
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
    
   //todo 在此處添加要操作code
   System.out.println("preHandle");
    return true; //todo 此處為false時(shí),請(qǐng)求不會(huì)到達(dá)control層
  }
 
  @Override
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle"); //todo 可以用來修改信息,跳轉(zhuǎn)等
  }
 
  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    System.out.println("afterCompletion"); //todo 最后執(zhí)行
  }
}

另一種攔截器:大同小異

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.hbut.interceptor;
 
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;
 
/**
 * @Author XiJun.Gong
 * @DATE 2016/6/1.
 * aim:  com.hbut.interceptor
 */
public class Test2Interceptor implements WebRequestInterceptor {
  @Override
  public void preHandle(WebRequest webRequest) throws Exception {
  }
 
  @Override
  public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
 
  }
 
  @Override
  public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
 
  }
}

過濾器: 依賴于servlet容器,使用回調(diào)函數(shù),過濾范圍大

攔截器: 依賴于框架容器 比如spring、mybatis ,靈活

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/gongxijun/p/5551542.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区视频精品 | 免费国产在线精品 | 久久精品日产第一区二区三区 | 午夜精品区 | 成人福利在线播放 | 成人在线视频一区 | 久久精品亚洲成在人线av网址 | 黄色一级片在线观看 | 日本网站在线看 | 日韩在线激情 | 久久国产精品久久久久 | 欧美成人精品一区 | 欧美高清另类自拍视频在线看 | 成人一区二区三区在线 | 久久亚洲一区二区三区成人国产 | 日产精品久久久一区二区福利 | 精国品产一区二区三区有限公司 | 欧美成人一区二区三区电影 | 久久777国产线看观看精品 | 日本a v免费观看 | 精品国产一区二区三区天美传媒 | 欧美成人三级大全 | 88xx成人精品视频 | 黄片一级毛片 | 369看片你懂的小视频在线观看 | 99国产精品自拍 | 亚洲情av| 色婷婷久久久久久 | 午夜视频亚洲 | 国产亚洲精品视频中文字幕 | 日本网站在线播放 | 亚洲午夜不卡 | 夜添久久精品亚洲国产精品 | 成人午夜在线观看视频 | 久久精品成人 | a黄在线观看 | 色人阁五月天 | 主人在调教室性调教女仆游戏 | 视频一区二区精品 | 嗯哈~不行好大h双性 | 92看片淫黄大片欧美看国产片 |