【SpringMVC】@RequestMapping注解


@RequestMapping注解的源碼

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

@RequestMapping注解的功能

@RequestMapping注解的作用就是將請求和處理請求的控制器方法關聯起來,建立映射關系。

SpringMVC 接收到指定的請求,就會來找到在映射關系中對應的控制器方法來處理這個請求。
該注解可以標識在類和方法上。

@RequestMapping注解的位置

@RequestMapping標識一個類:設置映射請求的請求路徑的初始信息

@RequestMapping標識一個方法:設置映射請求請求路徑的具體信息

如果有請求路徑的初始信息,則先設置請求路徑的初始信息才能設置請求路徑的具體信息。

比如,想從index的超鏈接跳轉到welcome頁面:

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<body>
    <h1>首頁</h1>
    <a th:href="@{/testRequestMapping}">訪問welcome頁面 </a>
</body>
</html>

index的超鏈接跳轉的路徑是/testRequestMapping

welcome.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>welcome</title>
</head>
<body>
<h1>Welcome to My Blog</h1>
</body>
</html>

welcome的控制器的類和方法上都加上RequestMapping注解

@Controller
@RequestMapping("/hello")
public class RequestMappingController {

    @RequestMapping("/testRequestMapping")
    public String welcome(){
        return "welcome";
    }
}

結果就是跳轉失敗
image

如果修改跳轉路徑為/hello/testRequestMapping,即可成功

<a th:href="@{/hello/testRequestMapping}">訪問welcome頁面 </a>

image

@RequestMapping注解的value屬性

@RequestMapping注解的value屬性通過請求的請求地址匹配請求映射

@RequestMapping注解的value屬性是一個字符串類型的數組,表示該請求映射能夠匹配多個請求地址所對應的請求

@RequestMapping注解的value屬性必須設置,至少通過請求地址匹配請求映射

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<body>
    <h1>首頁</h1>
    <a th:href="@{/testRequestMapping}">通過/testRequestMapping訪問welcome頁面 </a>
    <br>
    <a th:href="@{/test}">通過/test訪問welcome頁面 </a>
</body>
</html>
@Controller
public class RequestMappingController {

    @RequestMapping(value = {"/testRequestMapping","/test"})
    public String welcome(){
        return "welcome";
    }
}

image

兩個超連接都可訪問成功

@RequestMapping注解的method屬性

@RequestMapping注解的method屬性通過請求的請求方式(getpost)匹配請求映射

@RequestMapping注解的method屬性是一個RequestMethod類型的數組,表示該請求映射能夠匹配多種請求方式的請求

若當前請求的請求地址滿足請求映射的value屬性,但是請求方式不滿足method屬性,則瀏覽器報錯405:Request method 'POST' not supported

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<body>
    <form th:action="@{/test}" method="post">
        <input type="submit" value="以post方式提交">
    </form>
    <br>
    <form th:action="@{/test}" method="get">
        <input type="submit" value="以get方式提交">
    </form>
</body>
</html>

welcome.html的控制器

    @RequestMapping(value = "/test",
    method = {RequestMethod.GET})
    public String welcome(){
        return "welcome";
    }

如果不設置控制器的method,則indexpostget兩種方式都可以訪問welcome.html

如果設置控制器的methodRequestMethod.GET,則只能用get方式訪問

post方式訪問:

image

以get方式訪問:

image

對於處理指定請求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解

處理get請求的映射-->@GetMapping

處理post請求的映射-->@PostMapping

處理put請求的映射-->@PutMapping

處理delete請求的映射-->@DeleteMapping

免去了設置method的麻煩,只寫映射路徑即可

    @GetMapping("/test")
    public String welcome(){
        return "welcome";
    }

常用的請求方式有getpostputdelete

但是目前瀏覽器只支持getpost,若在form表單提交時,為method設置了其他請求方式的字符串(putdelete),則按照默認的請求方式get處理

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首頁</title>
</head>
<body>
    <form th:action="@{/test}" method="put">
        <input type="submit" value="以put方式提交">
    </form>
    <br>
</body>
</html>
    @PostMapping("/test")
    public String welcome(){
        return "welcome";
    }

image

@RequestMapping注解的params屬性

@RequestMapping注解的params屬性通過請求的請求參數匹配請求映射

@RequestMapping注解的params屬性是一個字符串類型的數組,可以通過四種表達式設置請求參數和請求映射的匹配關系

"param":要求請求映射所匹配的請求必須攜帶param請求參數

"!param":要求請求映射所匹配的請求必須不能攜帶param請求參數

"param=value":要求請求映射所匹配的請求必須攜帶param請求參數且param=value

"param!=value":要求請求映射所匹配的請求必須攜帶param請求參數但是param!=value

<a th:href="@{/test(username='admin',password=123456)}">測試@RequestMapping的params屬性-->/test</a><br>
@RequestMapping(
        value = {"/testRequestMapping", "/test"}
        ,method = {RequestMethod.GET, RequestMethod.POST}
        ,params = {"username","password!=123456"}
)
public String testRequestMapping(){
    return "welcome";
}

image

@RequestMapping注解的headers屬性

@RequestMapping注解的headers屬性通過請求的請求頭信息匹配請求映射

@RequestMapping注解的headers屬性是一個字符串類型的數組,可以通過四種表達式設置請求頭信息和請求映射的匹配關系

"header":要求請求映射所匹配的請求必須攜帶header請求頭信息

"!header":要求請求映射所匹配的請求必須不能攜帶header請求頭信息

"header=value":要求請求映射所匹配的請求必須攜帶header請求頭信息且header=value

"header!=value":要求請求映射所匹配的請求必須攜帶header請求頭信息且header!=value

若當前請求滿足@RequestMapping注解的valuemethod屬性,但是不滿足headers屬性,此時頁面顯示404錯誤,即資源未找到

SpringMVC路徑的模糊匹配

:表示任意的單個字符

    @RequestMapping(value = "/a?a/test")
    public String welcome(){
        return "welcome";
    }

*:表示任意的0個或多個字符

    @RequestMapping(value = "/a*a/test")
    public String welcome(){
        return "welcome";
    }

/**:表示任意的一層或多層目錄

    @RequestMapping(value = "/**/test")
    public String welcome(){
        return "welcome";
    }
  • 注意:
    • 在使用/**時,只能使用/**/xxx的方式

    • ?*不可以用 / ?來占位

SpringMVC支持路徑中的占位符(重點)

原始方式:/deleteUser?id=1

rest方式:/deleteUser/1

SpringMVC路徑中的占位符常用於RESTful風格中,當請求路徑中將某些數據通過路徑的方式傳輸到服務器中,就可以在相應的@RequestMapping注解的value屬性中通過占位符{xxx}表示傳輸的數據,在通過@PathVariable注解,將占位符所表示的數據賦值給控制器方法的形參

<a th:href="@{/testRest/1/admin}">測試路徑中的占位符-->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
    System.out.println("id:"+id+",username:"+username);
    return "success";
}
//最終輸出的內容為-->id:1,username:admin


免責聲明!

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



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