標籤:Null 字元串 gpo 支援 路徑 根據 嚴格 delattr 訪問 處理
Spring MVC中用於參數綁定的註解有很多,都在org.springframework.web.bind.annotation包中,根據它們處理的request的不同內容部分可以分為四類(主要講解常用類型)
>>處理request body 部分的註解: @RequestParam,@RequestBody
>>處理reuqest url 部分的註解: @PathVariable
>>處理request header 部分的註解: @RequestHeader,@CookieValue
>>處理attribute類型的註解: @SessionAttibute,@ModelAttribute
@RequestMapping註解:
**@RequestMapping雖然也在org.springframework.web.bind.annotation下面,但是嚴格來說,他並不屬於參數綁定註解
開發人員需要在控制器內部為每一個請求動作開發相應的處理方法。org.springframework.web.bind.annotation.RequestMapping註解類型指示Spring用哪一個類或方法來處理請求動作,該註解可用於類或方法。
@RequestMapping可以用來注釋一個控制器類,在這種情況下,所有方法都將映射為相對於類層級的請求,表示該控制器處理的所有請求都被映射到value屬性所指示的路徑下
@Controller@RequestMapping(value = "/user")public class UserController{ @RequestMapping(value = "/register") public String register(){ return "register"; } @RequestMapping(value = "/login") public String login(){ return "login"; } }
請求URL : http://localhost:8080/user/register
http://localhost:8080/user/login
>>1.value屬性
@RequestMapping(value = "/hello")public ModelAndView hello(){ return ...;}
該執行個體使用@RequestMapping注釋的value屬性將URL映射到方法上,在這個例子中,將hello映射到hello方法上,使用如下URL訪問應用時將由hello方法進行處理:
http://localhost:8080/context/hello
由於value屬性是@RequestMapping注釋的預設屬性,因此,如果只有唯一的屬性,則可以省略屬性名稱
@RequestMapping(value = "/hello")@RequestMapping("/hello")
但如果超過一個屬性,就必須寫上value屬性名稱
value屬性的值也可以是一個Null 字元串,此時該方法被映射到如下請求URL:
http://localhost:8080/context
>>2.method屬性
該屬性用來指示該方法僅僅處理那些HTTP請求方式
@RequestMapping(value = "/hello" method = "RequestMethod.POST")
以上代碼錶示該方法僅支援POST請求
也可以同時支援多個HTTP請求方式:
@RequestMapping(value = "/hello",method = {RequestMethod.POST,RequestMethod.GET})
如果沒有指定method屬性值,則請求處理方法可以處理任意的HTTP請求方式
@RequestMapping註解