2、@RequestMapping注解的用法


@RequestMapping有如下屬性值:

1、@RequestMapping來映射URL
    注解 @RequestMapping 可以用在類定義處和方法定義處。
    類定義處:規定初步的請求映射,相對於web應用的根目錄;
    方法定義處:進一步細分請求映射,相對於類定義處的URL。如果類定義處沒有使用該注解,則方法標記的URL相對於根目錄而言;

package com.springmvc.helloworld_1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value="/example")
public class HelloWorld {
    
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("hello world");
        
        return "success";
    }
}

  上面代碼在類定義處指定映射為"/example",在hello()方法處指定為"/helloworld"。那么hello()方法的URL映射地址為:http://localhost:8080/springMVC/example/helloworld

  如果去掉類定義處的@RequestMapping(value="/example"),那么hello()方法的映射地址就變為了:http://localhost:8080/springMVC/helloworld

  還有一個注意的@RequestMapping的默認屬性為value,所以@RequestMapping(value="/example")和@RequestMapping("/example")是等價的


    
2、@RequestMapping除了可以指定URL映射外,還可以指定“請求方法、請求參數和請求頭”的映射請求
    注解的value、method、params及headers分別指定“請求的URL、請求方法、請求參數及請求頭”。它們之間是與的關系,聯合使用會使得請求的映射更加精細。
  2.1  method屬性可以指定請求的類型,http中規定請求有四種類型:get,post,put,delete。其值在枚舉類型RequestMethod中有規定。

    例如:@RequestMapping(value="/helloworld", method=RequestMethod.DELETE) 指定只有DELETE方式的helloworld請求才能夠執行該處理方法。

  2.2 params和headers支持簡單的表達式:
       ——  params1:表示請求必須包含名為params1的請求參數
       ——  !params1:表示請求不能包含名為params1的請求參數
       ——  params1 != value1:表示請求必須包含名為params1的請求參數,但是其值不能是value1
       ——  {"params1 = value1", "param2"}:表示請求必須包含名為params1和params2兩個請求參數,且params1的值必須為value1

  2.3 Ant風格資源地址支持3種通配符:
       —— ?   : 匹配文件名中的一個字符
       —— *   : 匹配文件名中的任意多個字符(至少有一個字符)
       —— ** : 匹配多層路徑(至少有一層)

    @RequestMapping支持Ant風格的URL:

     —— /user/create??           匹配/user/createAA、/user/createBB等URL (??匹配任意兩個字符)
       —— /user/*/createUser     匹配/user/aaa/createUser、/user/bbb/createUser等URL (*匹配任意字符)
       —— /user/**/createUser   匹配/user/createUser、/user/aaa/bbb/createUser等URL (**匹配任意多層路徑)

    注意:其?和*必須要有,如果為空,則不符合    
    
  2.4 @PathVariable 映射URL綁定的占位符
      可以在控制器處理方法的入參中使用 @PathVariable 獲取到URL中占位符參數。 URL中的{xxx}占位符可以通過 @PathVariable("xxx") 綁定到操作方法的入參中。

@RequestMapping("/delete/{id}")
public String testPathVariable(@PathVariable("id") Integer id){
    System.out.println("id = " + id);
        
    return SUCCESS;
}

  

  下面是示例類將上面的知識點做了總結和應用示范:

package com.springmvc.RequestMapping_2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/springmvc")
public class RequestMappingTest {
    
    private static final String SUCCESS = "success"; 
    
    /**
     * 注解 @RequestMapping 可以用在類定義處和方法定義處
     * 1、類定義處:規定初步的請求映射,相對於web應用的根目錄
     * 2、方法定義處:進一步細分請求映射,相對於類定義處的URL。如果類定義處沒有使用該注解,則方法標記的URL相對於根目錄而言
     * 
     * 所以,testRequestMappingURL方法對應的URL目錄為:/springmvc/testRequestMappingURL
     */
    @RequestMapping("/testRequestMappingURL")
    public String testRequestMappingURL(){
        System.out.println("testRequestMappingURL 方法...");
        
        return SUCCESS;
    }
    
    /**
     * 1、了解:可以指定params和headers參數。
     * 
     * params和headers的值規定了:
     * ①、請求參數必須包含param,和view。而且,view的值必須為true
     * ②、請求頭中必須包含有Accept-Language,而且其值必須為zh-CN,zh;q=0.8
     */
    @RequestMapping(value="/testParamsAndHearders", 
                    params={"view=true","param"}, 
                    headers={"Accept-Language=zh-CN,zh;q=0.8"})
    public String testParamsAndHearders(){
        System.out.println("testParamsAndHearders 方法...");
        
        return SUCCESS;
    }
    
    /**
     * 2、Ant風格的占位符。
     * —— ?  : 匹配文件名中的一個字符
     * —— *  : 匹配文件名中的任意個字符(至少有一個)
     * —— ** : 匹配多層路徑(至少有一層)
     */
    @RequestMapping(value="/*/testAnt??")
    public String testAntPath(){
        System.out.println("testAntPath 方法...");
        
        return SUCCESS;
    }
    
    /**
     * 3、通過method指定請求方式必須是POST請求
     */
    @RequestMapping(value="/testMethod", method=RequestMethod.POST)
    public String testMethod(){
        System.out.println("testMethod 方法...");
        
        return SUCCESS;
    }
    
    /**
     * 4、可以使用注解@PathVariable("id")將@RequestMapping中的參數提取出來傳遞到方法的入參中
     */
    @RequestMapping("/delete/{id}")
    public String testPathVariable(@PathVariable("id") Integer id){
        System.out.println("id = " + id);
        
        return SUCCESS;
    }
}

 


    
   


免責聲明!

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



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