@RequestMapping
注解可以將HTTP請(qǐng)求映射給controller
來處理蝇闭,包括返回視圖頁面的controller和Rest
服務(wù)的controller梆造。
@RequestMapping
可以添加到類或者方法上儿奶。
@Controller
@RequestMapping(value = "/", method = RequestMethod.GET)
public class ContactController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String redirectToContactPage() {
return "redirect:contact";
}
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public String toAdminPage() {
return "admin";
}
@RequestMapping(value = "/contact", method = RequestMethod.GET)
public String toContactForOhersPage() {
return "contact";
}
}
在上面的例子中宝与,@RequestMapping
加在了類和方法上:
- 訪問
/
的請(qǐng)求會(huì)被ContactController
的redirectToContactPage()
方法處理 - 訪問
/admin
的請(qǐng)求會(huì)被ContactController
的toAdminPage()
方法處理
@RequestMapping
映射多個(gè)URL
@Controller
@RequestMapping("/")
public class ContactController {
@RequestMapping(value={"", "/page", "page*","view/*"})
String multipleMapping(){
return "Hello";
}
}
訪問下列地址都會(huì)被ContactController
的multipleMapping
方法處理:
localhost:8080/
localhost:8080/page
localhost:8080/pagehello
localhost:8080/view/
localhost:8080/view/view1
@RequestParam
請(qǐng)求參數(shù)
使用@RequestParam
可以將HTTP請(qǐng)求參數(shù)綁定到controller中方法的參數(shù)上喜滨,例如
@Controller
@RequestMapping("/")
public class ContactController {
@RequestMapping(value=/hello)
String sayHelloToUser(@RequestParam("username") String username){
return "Hello " + username;
}
@RequestMapping(value=/hi)
String sayHiToUser(@RequestParam String username){
return "Hello " + username;
}
}
- 當(dāng)訪問
localhost:8080/hello?username=ted
時(shí)捉捅,值ted
會(huì)綁定到參數(shù)username
上,結(jié)果是Hello ted
虽风。 - 當(dāng)HTTP請(qǐng)求參數(shù)和controller方法的參數(shù)名稱相同時(shí)棒口,可以省略,如
sayHiToUser
方法 -
@RequestParam
默認(rèn)要求參數(shù)是必要的辜膝,通過設(shè)置@RequestParam(value = "username", required = false)
設(shè)為可選无牵,這樣HTTP請(qǐng)求不帶username
參數(shù)也是可以訪問到指定的方法。 - 當(dāng)HTTP請(qǐng)求不帶
username
參數(shù)時(shí)厂抖,還可以設(shè)置它的默認(rèn)值茎毁,如@RequestParam(value = "username", defaultValue = "mattie")
指定HTTP請(qǐng)求類型
HTTP請(qǐng)求有GET
, POST
, PUT
, DELETE
等忱辅,@RequestMapping
可以處理特定的請(qǐng)求類型七蜘。相當(dāng)于如下注解:
GetMapping
PostMapping
PutMapping
DeleteMapping
@Controller
@RequestMapping("/")
public class ContactController {
@RequestMapping(value=/hello, method = RequestMethod.GET)
String sayHelloToUser(@RequestParam("username") String username){
return "Hello " + username;
}
@PostMapping(value=/hello)
String login(@RequestParam("password") String password){
return "Login Success!";
}
}
上面的例子中login
方法處理POST
類型的請(qǐng)求。
@RequestMapping
處理動(dòng)態(tài)URL
和注解@PathVariable
聯(lián)合使用耕蝉,可以解析HTTP請(qǐng)求中的動(dòng)態(tài)URL崔梗。
@Controller
@RequestMapping("/")
public class ContactController {
@RequestMapping(value = "/contacts/{contactname}", method = RequestMethod.GET)
String getContactName(@PathVariable("contactname") String name){
return "Contact name is " + name;
}
}
上面的例子中,訪問localhost:8080/contacts/ted
和localhost:8080/contacts/mattie
都會(huì)被ContactController
的getContactName
方法處理垒在,結(jié)果分別是Contact name is ted
和Contact name is mattie
蒜魄。
動(dòng)態(tài)URL也可以使用正則來匹配:
@Controller
@RequestMapping("/")
public class ShopController {
@RequestMapping(value = "/{id:[a-z]+}/{productname}", method = RequestMethod.GET)
String getProductName(@PathVariable("productname") String productName){
return "Product name is " + productName;
}
}
- 上面的例子中,訪問
localhost:8080/wears/shoes
和localhost:8080/foods/bread
都會(huì)被ShopController
的getProductName
方法處理 - 但是訪問
localhost:8080/101/fun
不會(huì)被處理场躯。
注意
@RequestParam
和@PathVariable
的區(qū)別:@RequestParam
解析URL中特定的請(qǐng)求參數(shù)的值谈为;而@PathVariable
用來匹配URL路徑的規(guī)則和模式。