@RequestMapping
注解可以將HTTP請求映射給controller
來處理,包括返回視圖頁面的controller和Rest
服務的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
加在了類和方法上:
- 訪問
/
的請求會被ContactController
的redirectToContactPage()
方法處理 - 訪問
/admin
的請求會被ContactController
的toAdminPage()
方法處理
@RequestMapping
映射多個URL
@Controller
@RequestMapping("/")
public class ContactController {
@RequestMapping(value={"", "/page", "page*","view/*"})
String multipleMapping(){
return "Hello";
}
}
訪問下列地址都會被ContactController
的multipleMapping
方法處理:
localhost:8080/
localhost:8080/page
localhost:8080/pagehello
localhost:8080/view/
localhost:8080/view/view1
@RequestParam
請求參數
使用@RequestParam
可以將HTTP請求參數綁定到controller中方法的參數上,例如
@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;
}
}
- 當訪問
localhost:8080/hello?username=ted
時,值ted
會綁定到參數username
上,結果是Hello ted
。 - 當HTTP請求參數和controller方法的參數名稱相同時,可以省略,如
sayHiToUser
方法 -
@RequestParam
默認要求參數是必要的,通過設置@RequestParam(value = "username", required = false)
設為可選,這樣HTTP請求不帶username
參數也是可以訪問到指定的方法。 - 當HTTP請求不帶
username
參數時,還可以設置它的默認值,如@RequestParam(value = "username", defaultValue = "mattie")
指定HTTP請求類型
HTTP請求有GET
, POST
, PUT
, DELETE
等,@RequestMapping
可以處理特定的請求類型。相當于如下注解:
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
類型的請求。
@RequestMapping
處理動態URL
和注解@PathVariable
聯合使用,可以解析HTTP請求中的動態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
都會被ContactController
的getContactName
方法處理,結果分別是Contact name is ted
和Contact name is mattie
。
動態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
都會被ShopController
的getProductName
方法處理 - 但是訪問
localhost:8080/101/fun
不會被處理。
注意
@RequestParam
和@PathVariable
的區別:@RequestParam
解析URL中特定的請求參數的值;而@PathVariable
用來匹配URL路徑的規則和模式。