springMVC學習(七)RESTful API和攔截器
RESTful API
RESTful結構:
-
每一個URI代表一種資源;
-
客戶端和伺服器之間,傳遞這種資源的某種表現層;
-
客戶端通過四個HTTP動詞,對伺服器端資源進行操作,實現"表現層狀態轉化"
非RESTful的http的url:http://localhost:8080/items/editItems.action?id=1&….
RESTful的url是簡潔的:http:// localhost:8080/items/editItems/1
RESTful結構開發步驟:
-
更改DispatcherServlet的配置,修改web.xml的url匹配模式
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-
配置對靜態資源的解析,當DispatcherServlet攔截/開頭的所有請求,對靜態資源的訪問就報錯,新增靜態資源配置
<!-- 靜態資源 解析 -->
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/img/" mapping="/img/**" />
/**
就表示不管有多少層,都對其進行解析,/*
代表的是當前層的所有資源
-
在Controller上使用PathVariable註解來繫結對應的引數
@PathVariable 可以將URL中佔位符引數{xx}繫結到處理器類的方法形參中@PathVariable(“xx“),兩個引數名必須相同
//根據商品id檢視商品資訊rest介面
//@RequestMapping中指定restful方式的url中的引數,引數需要用{}包起來
//@PathVariable將url中的{}中的引數和形參進行繫結
springMVC攔截器
使用者請求到DispatherServlet中,DispatherServlet呼叫HandlerMapping查詢Handler,HandlerMapping返回一個攔截鏈(多個攔截),springmvc中的攔截器是通過HandlerMapping發起的
開發步驟:
-
自定義攔截器,實現 HandlerInterceptor
public class HandlerInterceptor1 implements HandlerInterceptor {
//在執行handler之前來執行的
//用於使用者認證校驗、使用者許可權校驗
-
配置攔截器
<!--攔截器 -->
<mvc:interceptors>
<!--多個攔截器,順序執行 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="cn.itcast.ssm.controller.interceptor.HandlerInterceptor1"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="cn.itcast.ssm.controller.interceptor.HandlerInterceptor2"/>
</mvc:interceptor>
<mvc:interceptor>
<!-- /**可以攔截路徑不管多少層 -->
<mvc:mapping path="/**" />
<bean class="cn.itcast.ssm.controller.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
-
攔截器鏈是按配置的攔截器順序執行,執行preHandle是順序執行。執行postHandle、afterCompletion是倒序執行
-
如果preHandle不放行,postHandle、afterCompletion都不執行。只要有一個攔截器不放行,controller不能執行完成
-
只有前邊的攔截器preHandle方法放行,下邊的攔截器的preHandle才執行。
日誌攔截器或異常攔截器要求
-
將日誌攔截器或異常攔截器放在攔截器鏈中第一個位置,且preHandle方法放行
身份認證
攔截器
public class LoginInterceptor implements HandlerInterceptor {
//在執行handler之前來執行的
//用於使用者認證校驗、使用者許可權校驗
Controller