您好,登錄后才能下訂單哦!
本篇內容主要講解“SpringMVC攔截器的作用是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“SpringMVC攔截器的作用是什么”吧!
web.xml文件
springMVC配置文件:
controller類:
第一個攔截器配置:
第二個攔截器:
總結
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置springMvc中央處理器--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化的時候配置加載springmvc的配置文件--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 3、修改web.xml中的請求路徑地址方式--> <servlet-mapping> <servlet-name>springMVC</servlet-name> <!-- 3、修改web.xml中的請求路徑地址方式--> <url-pattern>/</url-pattern> <!-- <url-pattern>*.do</url-pattern>--> </servlet-mapping> <!-- 1、將css文件使用默認處理方式進行處理,不再交由中央控制器處理, 設置路徑的更高匹配優先級 --> <!-- <servlet-mapping>--> <!-- <servlet-name>default</servlet-name>--> <!-- <url-pattern>*.css</url-pattern>--> <!-- <url-pattern>*.js</url-pattern>--> <!-- <url-pattern>*.jpg</url-pattern>--> <!-- <url-pattern>*.png</url-pattern>--> <!-- <url-pattern>*.html</url-pattern>--> <!-- </servlet-mapping>--> </web-app>
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自動掃描包--> <!-- 開啟ioc 注解事務支持--> <context:component-scan base-package="cn"></context:component-scan> <!--開啟spiring mvc注解支持--> <mvc:annotation-driven></mvc:annotation-driven> <!--配置spring 中的視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:interceptors> <bean id="interceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean> <mvc:interceptor> <mvc:mapping path="/**"/> <bean id="interceptor1" class="cn.hp.interceptor.TestInterceptor1"></bean> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/test1.do"/> <mvc:exclude-mapping path="/test2.do"/> <bean id="interceptor2" class="cn.hp.interceptor.TestInterceptor2"></bean> </mvc:interceptor> </beans>
package cn.hp.action; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; @Controller public class UserAction { @RequestMapping("/test1.do") public String test01(){ System.out.println("正在執行test1這個業務邏輯"); return "index"; } @RequestMapping("/test2.do") public String test02(){ System.out.println("正在執行test2這個業務邏輯"); return "index"; } }
package cn.hp.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("第一個攔截器,執行業務邏輯之前執行此方法"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("第一個攔截器,執行業務邏輯之后,視圖渲染之前執行此方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("第一個攔截器,執行視圖渲染之后執行此方法"); } }
package cn.hp.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestInterceptor2 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("第二個攔截器,執行業務邏輯之前執行此方法"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("第二個攔截器,執行業務邏輯之后,視圖渲染之前執行此方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("第二個攔截器,執行視圖渲染之后執行此方法"); } }
到此,相信大家對“SpringMVC攔截器的作用是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。