SpringMVC——請求映射,springmvc映射

來源:互聯網
上載者:User

SpringMVC——請求映射,springmvc映射

SpringMVC中,如何處理請求是很重要的任務。請求映射都會使用@RequestMapping標註。其中,類上的標註相當於一個首碼,表示該處理器是處理同一類請求;方法上的標註則更加細化。如,類的標註可能是“user”,表示全部都是與使用者相關的操作;具體到方法可能有“create”“update”“delete”等,分別表示對使用者進行哪一類操作。

package cn.javass.chapter6.web.controller;  @Controller  @RequestMapping(value="/user")                 //①處理器的通用映射首碼  public class HelloWorldController2 {      @RequestMapping(value = "/hello2")        //②相對於①處的映射進行窄化      public ModelAndView helloWorld() {           //省略實現      }  }  

現在就來總結一下請求映射有哪些。

一、URL路徑映射

這種映射涉及的屬性只有value。

@RequestMapping(value={"/test1", "/user/create"}) //或,表示多個路徑都可以映射到同一個處理方法@RequestMapping(value="/users/{userId}/topics/{topicId}") //也可以使用大括弧,表示變數預留位置@RequestMapping(value="/product?") //可匹配“/product1”或“/producta”,但不匹配“/product”或“/productaa”@RequestMapping(value="/products/**/{productId}") //可匹配“/products/abc/abc/123”或“/products/123”@RequestMapping(value="/products/{categoryCode:\\d+}-{pageNumber:\\d+}") //支援Regex

二、要求方法映射限定

不僅要提供value屬性,還要提供method屬性。

@RequestMapping(value="/create", method = RequestMethod.GET) //表示可處理匹配“/create”且要求方法為“GET”的請求@RequestMapping(value="/create", method = RequestMethod.POST) //表示可處理匹配“/create”且要求方法為“POST”的請求

一般瀏覽器僅支援GET和POST類型,其他如PUT、DELETE等需要進行類比。

三、請求參數映射限定

需要提供params屬性和method屬性。

以下面的控制器為例,

@Controller  @RequestMapping("/parameter1")       //①處理器的通用映射首碼  public class RequestParameterController1 {    // Something...}
@RequestMapping(params="create", method=RequestMethod.GET) //表示請求中有“create”的參數名且要求方法為“GET”即可匹配,                                                             如可匹配的請求URL“http://×××/parameter1?create”@RequestMapping(params="create", method=RequestMethod.POST) //表示請求中有“create”的參數名且要求方法為“POST”即可匹配@RequestMapping(params="!create", method=RequestMethod.GET) //表示請求中沒有“create”參數名且要求方法為“GET”即可匹配,                                                              如可匹配的請求URL“http://×××/parameter1?abc”@RequestMapping(params="submitFlag=create", method=RequestMethod.GET) //表示請求中有“submitFlag=create”請求參數且要求方法為“GET”即可匹配,                                                                        如請求URL為http://×××/parameter2?submitFlag=create@RequestMapping(params="submitFlag=create", method=RequestMethod.POST) //表示請求中有“submitFlag=create”請求參數且要求方法為“POST”即可匹配@RequestMapping(params="submitFlag=create", method=RequestMethod.GET) //表示請求中有“submitFlag=create”請求參數且要求方法為“GET”即可匹配,                                                                        如請求URL為http://×××/parameter2?submitFlag=create

與value中的參數組合表示“或”不同,params參數組合表示“且”,即:

@RequestMapping(params={"test1", "test2=create"}) //表示請求中的有“test1”參數名 且 有“test2=create”參數即可匹配,如可匹配的請求URL“http://×××/parameter3?test1&test2=create

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.