@RequestMapping註解

來源:互聯網
上載者:User

標籤: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註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.