Spring MVC配置及攔截器的實現
如題所示,這裡主要是介紹攔截器的使用了但是為了更貼合實際的專案所以我們先匯入spring mvc框架。為了使用spring mvc我們要匯入相關的包,它依賴於spring-web 和spring -webmvc我這裡的版本是4.0.6的。將相關的包匯入到工程中並且新增編譯路徑後我們再在web.xml檔案中配置spring mvc。
前面說過配置一個基本的servlet時要配置和這兩個標籤,同理spring mvc也要配置這兩個標籤,因為spring mvc是對請求的集中處理分發在這之前它也需要接受到請求才行。這裡就不細說直接看程式碼和註釋吧。
<servlet>
<servlet-name >spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- spring-mvc具體的配置檔案 -->
<param-value >/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<!-- servlet載入的優先順序,當值大於等於0時在應用啟動時就載入這
個servlet,值越小優先順序越高。
當值為負數或未指定時servlet被使用時才載入 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name >spring</servlet-name>
<!-- spring-mvc能夠處理的請求要匹配如下格式 -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
現在具體看spring-mvc.xml這個schema檔案吧。
<?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:p="http://www.springframework.org/schema/p" 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">
<!-- 預設的註解對映支援 -->
<mvc:annotation-driven/>
<!-- 自動掃描使用了註解的包如使用了
@Controller、@Service這些註解的檔案必須在這個包下面 -->
<context:component-scan base-package="ServaceStudy"/>
<!-- 檢視解釋類 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 檢視名前面部分匹配的格式,可以理解為檢視檔案的目錄結構 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 檢視的檔案後面匹配的格式,簡單理解就是檢視檔案的字尾名 -->
<property name="suffix" value=".jsp"/>
<!-- 檢視的型別,這裡配置的是jsp。檢視型別有很多種具體可以根據專案需求配置不同的型別 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
<!-- 配置攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 攔截器攔截的URL格式 -->
<mvc:mapping path="/api/**" />
<!-- 處理攔截的具體實現類 -->
<bean class="ServaceStudy.TestInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
- 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
- 34
- 35
- 36
- 37
- 38
這樣配置就可以使用註解,分發請求,進行攔截了,具體的意思看註釋吧。以後根據需要我們還可以在這裡新增其他的配置比如上傳檔案就需要新增配置瞭如:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>104857600</value>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
見名知意我就不細說了
然後我們來測試一下, 首先必須實現這個類TestInterceptor,TestInterceptor實現了 HandlerInterceptor這個介面。這個介面有三個方法本別對應了處理請求之前,處理請求結束渲染圖層前和圖層渲染結束請求結束這三個階段。
boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception
- 1
- 2
這個是請求處理之前,如果返回true就繼續向下執行請求給與通過,如果返回false就攔截不給請求通過。handler這個引數是處理這個請求所在的Controller物件,request、response是請求的接受和返回物件。如此關於請求的很多資訊我們就可以在攔截器中獲取了。
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception
- 1
- 2
- 3
這是處理請求結束渲染圖層之前的階段,modelAndView這個引數是相信大家都很熟悉就是我們要渲染的圖層物件,所以我們可以在這裡改變一些資訊從而控制圖層。
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception
- 1
- 2
- 3
這是請求的最後階段了,通常在這個方法中會做一些收尾工作比如資源的釋放。看程式碼吧,
package ServaceStudy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class TestInterceptor 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("========處理請求後,渲染頁面前======");
modelAndView.addObject("post","interceptor change view before rendering");
}
@Override
public void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("========檢視渲染結束了,請求處理完畢====");
}
}
- 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 ServaceStudy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
//@Scope("prototype") 預設Controller是單例,所有的請求都是一個Controller。Scope宣告作用域,prototype原型
public class SpringMvcTest {
public Integer i = 1;
@RequestMapping("/api/test.html")
public void requestTest(HttpServletRequest request,HttpServletResponse response) {
System.out.println("==========");
}
//攔截器測試
@RequestMapping("/api/hellow.html")
public ModelAndView requestHellowWord(HttpServletRequest request,HttpServletResponse response) {
ModelAndView model = new ModelAndView("/hellow");
model.addObject("count",i++);
System.out.println("==========處理請求中=======");
return model;
}
}
- 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
還有個檢視檔案hellow.jsp
<h1>Test</h1>
<div>HTLLOWORD</div>
<div>count:${count}</div>
<div>interceptor:${post}</div>
- 1
- 2
- 3
- 4
- 5
- 6
今天想攔截所有的json請求,找了一圈沒找到解決方案,偶然看到stackoverflow上面的回覆,才解決了這個難題。
特貼出來共享。
直接在xml檔案中配置即可:
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mappingpath="/**/*.json"/>
- <!-- 定義在mvc:interceptor下面的表示是對特定的請求才進行攔截的 -->
- <beanclass="com.interceptor"/>
- </mvc:interceptor>
- </mvc:interceptors>