如下部份內容來自官方文檔
文檔描述html
使用@Controller註解定義一個控制器,@Controller註解代表了一個類是做爲控制器的角色而存在的。Spring不要求你去繼承任何控制器基類,也不要求你去實現Servlet的那套API。上一節講的DispatcherServlet會掃描全部註解了@Controller的類,檢測其中經過@RequestMapping註解配置的方法。
你可使用@RequestMapping註解來將請求URL,如/appointments等,映射到整個類上或某個特定的處理器方法上。通常來講,類級別的註解負責將一個特定(或符合某種模式)的請求路徑映射到一個控制器上,同時經過方法級別的註解來細化映射,即根據特定的HTTP請求方法(「GET」「POST」方法等)、HTTP請求中是否攜帶特定參數等條件,將請求映射到匹配的方法上。
@Controller @RequestMapping("/appointments") public class AppointmentsController { private final AppointmentBook appointmentBook; @Autowired public AppointmentsController(AppointmentBook appointmentBook) { this.appointmentBook = appointmentBook; } @RequestMapping(method = RequestMethod.GET) public Map<String, Appointment> get() { return appointmentBook.getAppointmentsForToday(); } @RequestMapping(path = "/{day}", method = RequestMethod.GET) public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) { return appointmentBook.getAppointmentsForDay(day); } @RequestMapping(path = "/new", method = RequestMethod.GET) public AppointmentForm getNewForm() { return new AppointmentForm(); } @RequestMapping(method = RequestMethod.POST) public String add(@Valid AppointmentForm appointment, BindingResult result) { if (result.hasErrors()) { return "appointments/new"; } appointmentBook.addAppointment(appointment); return "redirect:/appointments"; } }
在上面的示例中,許多地方都使用到了@RequestMapping註解。
1) 第一次使用點是做用於類級別的,它指示了全部/appointments開頭的路徑都會被映射到控制器下。
2) get()方法上的@RequestMapping註解對請求路徑進行了進一步細化:它僅接受GET方法的請求。這樣,一個請求路徑爲/appointments、HTTP方法爲GET的請求,將會最終進入到這個方法被處理。
3) add()方法也作了相似的細化,而getNewForm()方法則同時註解了可以接受的請求的HTTP方法和路徑。這種狀況下,一個路徑爲appointments/new、HTTP方法爲GET的請求將會被這個方法所處理。
4) 類級別的@RequestMapping註解並非必須的。
5) 不配置的話則全部的路徑都是絕對路徑,而非相對路徑。@RequestMapping註解默認會映射全部的HTTP請求方法。若是僅想接收某種請求方法,請在註解中指定之@RequestMapping(method=GET)以縮小範圍。
6) getForDay()方法則展現了使用@RequestMapping註解的另外一個技巧:URI模板。(下面會講到)
URI模板能夠爲快速訪問@RequestMapping中指定的URL的一個特定的部分提供很大的便利。URI模板是一個相似於URI的字符串,只不過其中包含了一個或多個的變量名。當你使用實際的值去填充這些變量名的時候,模板就退化成了一個URI。在URI模板的RFC提議中定義了一個URI是如何進行參數化的。好比說,一個這個URI模板 http://www.example.com/users/{userId}就包含了一個變量名userId。將值fred賦給這個變量名後,它就變成了一個URI: http://www.example.com/users/...。在Spring MVC中你能夠在方法參數上使用@PathVariable註解,將其與URI模板中的參數綁定起來:
public String findOwner(@PathVariable String ownerId, Model model) { Owner owner = ownerService.findOwner(ownerId); model.addAttribute("owner", owner); return "displayOwner"; }
URI模板"/owners/{ownerId}"指定了一個變量,名爲ownerId。當控制器處理這個請求的時候,ownerId的值就會被URI模板中對應部分的值所填充。好比說,若是請求的URI是/owners/fred,此時變量ownerId的值就是fred.
或者也能夠在註解中聲明,而後使用別名的參數名:
@RequestMapping(path="/owners/{ownerId}}", method=RequestMethod.GET) public String findOwner(@PathVariable("ownerId") String theOwner, Model model) { // 具體的方法代碼… }
一個方法能夠擁有任意數量的@PathVariable註解:
@RequestMapping(path="/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET) public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { Owner owner = ownerService.findOwner(ownerId); Pet pet = owner.getPet(petId); model.addAttribute("pet", pet); return "displayPet"; }
URI模板能夠從類級別和方法級別的 @RequestMapping 註解獲取數據。所以,像這樣的findPet()方法能夠被相似於/owners/42/pets/21這樣的URL路由並調用到:
_@Controller_ @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}") public void findPet(_@PathVariable_ String ownerId, _@PathVariable_ String petId, Model model) { // 方法實現體這裏忽略 } }
@PathVariable能夠被應用於全部 簡單類型 的參數上,好比int、long、Date等類型。Spring會自動地幫你把參數轉化成合適的類型,若是轉換失敗,就拋出一個TypeMismatchException。若是你須要處理其餘數據類型的轉換,也能夠註冊本身的類。同時你也能夠學習帶正則表達式的URL模板。