@RequestMapping注解的作用就是將請(qǐng)求的 URL 地址和處理請(qǐng)求的方式(handler方法)關(guān)聯(lián)起來(lái),建立映射關(guān)系。
SpringMVC 接收到指定的請(qǐng)求,就會(huì)來(lái)找到在映射關(guān)系中對(duì)應(yīng)的方法來(lái)處理這個(gè)請(qǐng)求。
1. 精準(zhǔn)路徑匹配
在@RequestMapping注解指定 URL 地址時(shí),不使用任何通配符,按照請(qǐng)求地址進(jìn)行精確匹配。
@Controller
public class UserController {
/**
* 精準(zhǔn)設(shè)置訪問(wèn)地址 /user/login
*/
@RequestMapping("/user/login")
@ResponseBody
public String login(){
System.out.println("UserController.login");
return "login success!!";
}
/**
* 精準(zhǔn)設(shè)置訪問(wèn)地址 /user/register
*/
@RequestMapping("/user/register")
@ResponseBody
public String register(){
System.out.println("UserController.register");
return "register success!!";
}
}
2. 模糊路徑匹配
在@RequestMapping注解指定 URL 地址時(shí),通過(guò)使用通配符,匹配多個(gè)類似的地址。
@Controller
public class ProductController {
/**
* 路徑設(shè)置為 /product/*
* /* 為單層任意字符串 /product/a /product/aaa 可以訪問(wèn)此handler
* /product/a/a 不可以
* 路徑設(shè)置為 /product/**
* /** 為任意層任意字符串 /product/a /product/aaa 可以訪問(wèn)此handler
* /product/a/a 也可以訪問(wèn)
*/
@RequestMapping("/product/*")
@ResponseBody
public String show(){
System.out.println("ProductController.show");
return "product show!";
}
}
注意:
-
*:只能匹配URL地址中的一層,如果想準(zhǔn)確匹配兩層,那么就寫(xiě) / */ * 以此類推。
-
** :可以匹配URL地址中的多層。
其中所謂的一層或多層是指一個(gè)URL地址字符串被“/”劃分出來(lái)的各個(gè)層次
3. 類和方法級(jí)別區(qū)別
@RequestMapping
注解可以用于類級(jí)別和方法級(jí)別,它們之間的區(qū)別如下:
- 設(shè)置到類級(jí)別:
@RequestMapping
注解可以設(shè)置在控制器類上,用于映射整個(gè)控制器的通用請(qǐng)求路徑。這樣,如果控制器中的多個(gè)方法都需要映射同一請(qǐng)求路徑,就不需要在每個(gè)方法上都添加映射路徑。 - 設(shè)置到方法級(jí)別:
@RequestMapping
注解也可以單獨(dú)設(shè)置在控制器方法上,用于更細(xì)粒度地映射請(qǐng)求路徑和處理方法。當(dāng)多個(gè)方法處理同一個(gè)路徑的不同操作時(shí),可以使用方法級(jí)別的@RequestMapping
注解進(jìn)行更精細(xì)的映射。
//1.標(biāo)記到handler方法
@RequestMapping("/user/login")
@RequestMapping("/user/register")
@RequestMapping("/user/logout")
//2.優(yōu)化標(biāo)記類+handler方法
//類上
@RequestMapping("/user")
//handler方法上
@RequestMapping("/login")
@RequestMapping("/register")
@RequestMapping("/logout")
下面的寫(xiě)法是等同上面的。當(dāng)類和方法都有映射路徑時(shí),那么一個(gè)方法的完整路徑就是類路徑 + 方法路徑。
4. 附帶請(qǐng)求方式限制
HTTP 協(xié)議定義了八種請(qǐng)求方式,在 SpringMVC 中封裝到了下面這個(gè)枚舉類:
public enum RequestMethod {
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}
默認(rèn)情況下:@RequestMapping(“/logout”) 任何請(qǐng)求方式都可以訪問(wèn)!
如果需要特定指定:
@Controller
public class UserController {
/**
* 精準(zhǔn)設(shè)置訪問(wèn)地址 /user/login
* method = RequestMethod.POST 可以指定單個(gè)或者多個(gè)請(qǐng)求方式!
* 注意:違背請(qǐng)求方式會(huì)出現(xiàn)405異常!
*/
@RequestMapping(value = "/user/login" , method = RequestMethod.POST)
@ResponseBody
public String login(){
System.out.println("UserController.login");
return "login success!!";
}
/**
* 精準(zhǔn)設(shè)置訪問(wèn)地址 /user/register
*/
@RequestMapping(value = "/user/register", method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String register(){
System.out.println("UserController.register");
return "register success!!";
}
}
注意:違背請(qǐng)求方式,會(huì)出現(xiàn)405異常?。?!
5. 進(jìn)階注解
還有 @RequestMapping
的 HTTP 方法特定快捷方式變體:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-840473.html
-
@GetMapping
等價(jià)于@RequestMapping(method = {RequestMethod.POST,RequestMethod.GET})
-
@PostMapping
等價(jià)于@RequestMapping(method = {RequestMethod.POST,RequestMethod.POST})
-
@PutMapping
等價(jià)于@RequestMapping(method = {RequestMethod.POST,RequestMethod.PUT})
-
@DeleteMapping
等價(jià)于@RequestMapping(method = {RequestMethod.POST,RequestMethod.DELETE})
注意:進(jìn)階注解只能添加到handler方法上,無(wú)法添加到類上!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-840473.html
到了這里,關(guān)于SpringMVC訪問(wèn)路徑設(shè)置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!