更新時間:2022-03-25 09:46:12 來源:動力節(jié)點(diǎn) 瀏覽1284次
Spring MVC 提供了對將攔截器配置到 Web 應(yīng)用程序中非常容易的支持。
SpringMVC攔截器配置需要以下步驟:
1.創(chuàng)建一個將充當(dāng)您的攔截器的類。
這個類應(yīng)該擴(kuò)展HandlerInterceptorAdapter類。
2.preHandle()根據(jù)攔截器的功能將此類的,postHandle()和方法實現(xiàn)afterCompletion()到您的類中。
3.preHandle()如果攔截器應(yīng)該在應(yīng)用程序處理請求之前添加一些功能,則需要方法。
如果此方法返回true,則將請求轉(zhuǎn)發(fā)給應(yīng)用程序,否則返回。
postHandle()如果攔截器在響應(yīng)發(fā)送到客戶端之前執(zhí)行任務(wù),則afterCompletion()方法是必需的,如果攔截器應(yīng)該在響應(yīng)發(fā)送到客戶端之后執(zhí)行某些操作,則方法是必需的。
這些方法都不是強(qiáng)制性的。
4.將您的攔截器類聲明為 spring 配置文件中標(biāo)記下的bean<mvc:interceptors>
。
假設(shè)您希望您的應(yīng)用程序響應(yīng) url 以 myapp 結(jié)尾的請求,否則返回錯誤頁面。
這可以使用攔截器輕松完成。
以下部分將指導(dǎo)您如何在 Spring 應(yīng)用程序中配置攔截器并實現(xiàn)此功能。
package com.codippa.interceptor.ApplicationInterceptor
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class ApplicationInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
// get the url of request
String uri = request.getRequestURI();
//check if it has myapp at the end
if (uri.endsWith("myapp")) {
//forward request to the application
return true;
}
// else show error page
response.sendRedirect("/error.jsp");
return false;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws IOException {
System.out.println("Request handled");
}
}
每個請求首先到達(dá)preHandle攔截器類的方法,該方法獲取請求到達(dá)的 uri。
它檢查 uri 是否以字符串 myapp 結(jié)尾并相應(yīng)地返回true或返回false。
請注意,如果preHandle攔截器的方法返回true,則將請求轉(zhuǎn)發(fā)給應(yīng)用程序,否則將其返回。
在應(yīng)用程序處理請求后,它到達(dá)postHandle輸出字符串請求處理的方法,該字符串打印在服務(wù)器日志文件中。
在應(yīng)用程序的 Spring 上下文文件中將此類聲明為 bean,如下所示:
<?xml 版本= “1.0” 編碼= “UTF-8” ?>
<beans xmlns= “http://www.springframework.org/schema/beans”
xmlns:context= “http://www.springframework.org /schema/context”
xmlns:xsi= “http://www.w3.org/2001/XMLSchema-instance” xmlns:mvc= “http://www.springframework.org/schema/mvc”
xmlns:tx= “ http://www.springframework.org/schema/tx” xmlns:p= “http://www.springframework.org/schema/p”
xsi:schemaLocation= “
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context。 xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd” >
<mvc:interceptors>
<bean class=”com.codippa.interceptor.ApplicationInterceptor” />
</mvc:interceptors>
<bean
class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/WEB-INF/views/jsp/" />
<property name= "suffix" value= " .jsp” />
</bean>
</beans>
以上就是關(guān)于“SpringMVC攔截器配置示例”的介紹,大家如果想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的SpringMVC教程,里面的課程內(nèi)容更加細(xì)致全面,由淺到深,通俗易懂,適合沒有基礎(chǔ)的小白學(xué)習(xí),希望對大家能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743