SpringMVC學習 -- 使用 @RequestMapping 映射請求


在控制器的類定義及方法出定義出都可以標注 @RequestMapping:

  • 類定義處:提供初步的請求映射信息。相對於 Web 應用的根目錄。
  • 方法定義出:提供進一步的細分映射信息。相對於類定義處的 URL。若類定義處未標注 @RequestMapping , 則方法定義處標記的 URL 相對於 Web 應用的根目錄。

DispatcherServlet 截獲請求后 , 就通過控制器上 @RequestMapping 提供的映射信息確定請求所對應的處理方法。

web.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5          version="3.1">
 6 
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>/WEB-INF/applicationContext.xml</param-value>
10     </context-param>
11     <listener>
12         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13     </listener>
14     <servlet>
15         <servlet-name>dispatcher</servlet-name>
16         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
17         <load-on-startup>1</load-on-startup>
18     </servlet>
19     <servlet-mapping>
20         <servlet-name>dispatcher</servlet-name>
21         <url-pattern>/</url-pattern>
22     </servlet-mapping>
23 </web-app>

dispatcher-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        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">
 6     <!-- 掃描自定義包 -->
 7     <context:component-scan base-package="com.itdoc.springmvc"/>
 8 
 9     <!-- 配置試圖解析器: 把 Controller 返回值解析成實際的物理視圖 -->
10     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11         <property name="prefix" value="/WEB-INF/views/"/>
12         <property name="suffix" value=".jsp"/>
13     </bean>
14 </beans>

TestRequestMapping.java

 1 package com.itdoc.springmvc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 /**
 9  * @BLOG http://www.cnblogs.com/goodcheap
10  * @DESCRIBE RequestMapping 測試
11  * @AUTHOR WángChéngDá
12  * @DATE 2017-03-08 14:30
13  */
14 @Controller
15 @RequestMapping("/springmvc")
16 public class TestRequestMapping {
17 
18     private final static String SUCCESS = "success";
19 
20     /**
21      * 1.@RequestMapping 除了修飾方法還可以修飾類。
22      * 2.修飾類和修飾方法
23      * 1) 修飾類: 提供初步的請求映射信息, 相對於 WEB 應用的根目錄。
24      * 2) 修飾方法: 提供進一步細化的請求映射信息, 相對於修飾類處的 URL。
25      * 若修飾類處未標注 @RequestMapping, 則修飾方法處的 URL 相對於 WEB 應用的根目錄。
26      */
27     @RequestMapping("/testreqmap")
28     public String testReqMap() {
29         System.out.println("I am TestRequestMapping's testReqMap method...");
30         return SUCCESS;
31     }
32 }

 

映射請求參數、請求方式或請求頭

 

@RequestMapping 的 value , method , params 及 headers 分別表示請求 URL , 請求方式 , 請求參數及請求頭的映射條件 , 聯合使用多個條件可讓請求映射更加精確化。

method 請求方式常用有四種:

  • method = RequestMethod.POST
  • method = RequestMethod.GET
  • method = RequestMethod.PUT
  • method = RequestMethod.DELETE
 1 package com.itdoc.springmvc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 /**
 9  * @BLOG http://www.cnblogs.com/goodcheap
10  * @DESCRIBE RequestMapping 測試
11  * @AUTHOR WángChéngDá
12  * @DATE 2017-03-08 14:30
13  */
14 @Controller
15 @RequestMapping("/springmvc")
16 public class TestRequestMapping {
17 
18     private final static String SUCCESS = "success";
19 
20     /**
21      * 使用 method 屬性來制定請求方式。
22      *
23      * @return
24      */
25     @RequestMapping(value = "/testMethod", method = RequestMethod.POST)
26     public String testMethod() {
27         System.out.println("I am TestRequestMapping's testMethod method...");
28         return SUCCESS;
29     }
30 }

 

params 和 headers 支持簡單的表達式:

  •  param:表示請求必須包含名為 param 的請求參數。
  • !param:表示請求不能包含名為 param 的請求參數。
  • param != value: 表示請求包含名為 param 的請求參數 , 但其值不能為 value。
  • params 可以有多個參數 , 用逗號隔開。示例:params = {"username", "age!=20"}
 1 package com.itdoc.springmvc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 /**
 9  * @BLOG http://www.cnblogs.com/goodcheap
10  * @DESCRIBE RequestMapping 測試
11  * @AUTHOR WángChéngDá
12  * @DATE 2017-03-08 14:30
13  */
14 @Controller
15 @RequestMapping("/springmvc")
16 public class TestRequestMapping {
17 
18     private final static String SUCCESS = "success";
19 
20     /**
21      * 可以使用 params 和 headers 來更加精確的映射請求, params 和 headers 支持簡單的表達式。
22      * @return
23      */
24     @RequestMapping(value = "testParamsAndHeaders", params = {"username", "age!=20"},
25             headers = {"Accept-Language=zh-CN,zh;q=0.8"})
26     public String testParamsAndHeaders() {
27         System.out.println("I am TestRequestMapping's testParamsAndHeaders method...");
28         return SUCCESS;
29     }
30 }

 

Ant 風格資源地址支持3種匹配符:

  • ?:匹配文件名中的一個字符。
  • *:匹配文件名中的任意字符。
  • **:匹配多層路徑。
 1 package com.itdoc.springmvc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 /**
 9  * @BLOG http://www.cnblogs.com/goodcheap
10  * @DESCRIBE RequestMapping 測試
11  * @AUTHOR WángChéngDá
12  * @DATE 2017-03-08 14:30
13  */
14 @Controller
15 @RequestMapping("/springmvc")
16 public class TestRequestMapping {
17 
18     private final static String SUCCESS = "success";
19 
20     @RequestMapping("/testAndPath/*/abc")
21     public String testAndPath() {
22         System.out.println("I am TestRequestMapping's testAntPath method...");
23         return SUCCESS;
24     }
25 }

 

@PathVariable 映射 URL 綁定占位符:

  • 帶占位符的 URL 是 Spring3.0 新增的功能 , 該功能在 SpringMVC 的 REST 目標挺進發展過程中具有里程碑的意義。
  • 通過 @PathVariable 可以將 URL 中占位符參數綁定到控制器處理方法的入參中:URL 中的{xxx} 占位符可以通過 @PathVariable("xxx") 綁定到操作方法的入參中。
  • 注意:@PathVariable("xxx") 中的 xxx 必須與占位符 {xxx} 中的 xxx 相同。 
 1 package com.itdoc.springmvc;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.PathVariable;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RequestMethod;
 7 
 8 /**
 9  * @BLOG http://www.cnblogs.com/goodcheap
10  * @DESCRIBE RequestMapping 測試
11  * @AUTHOR WángChéngDá
12  * @DATE 2017-03-08 14:30
13  */
14 @Controller
15 @RequestMapping("/springmvc")
16 public class TestRequestMapping {
17 
18     private final static String SUCCESS = "success";
19 
20     /**
21      * @PathVariable 可以來映射 URL 中的占位符到目標方法的參數中。
22      * @param id
23      * @return
24      */
25     @RequestMapping("/testPathVariable/{id}")
26     public String testPathVariable(@PathVariable("id") Integer id) {
27         System.out.println("I am TestRequestMapping's testPathVariable method...\t id=" + id);
28         return SUCCESS;
29     }
30 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM