URL路径映射

来源:互联网 发布:知乎日语自学用书 编辑:程序博客网 时间:2024/06/06 19:15

URL路径映射

  • 普通URL路径映射 
    @RequestMapping(value={“/test1”, “/user/create”}):多个URL路径可以映射到同一个处理器的功能处理方法。
  • URI模板模式映射 
    @RequestMapping(value=”/users/{userId}”):{×××}占位符, 请求的URL可以是 “/users/123456”或 
    “/users/abcd”,通过@PathVariable可以提取URI模板模式中的{×××}中的×××变量。 
    @RequestMapping(value=”/users/{userId}/create”):这样也是可以的,请求的URL可以是“/users/123/create”。 
    @RequestMapping(value=”/users/{userId}/topics/{topicId}”):这样也是可以的,请求的URL可以是“/users/123/topics/123”。
  • Ant风格的URL路径映射 
    @RequestMapping(value=”/users/**”):可以匹配“/users/abc/abc”,但“/users/123”将会被【URI模板模式映射中的“/users/{userId}”模式优先映射到】【最长匹配优先】。 
    @RequestMapping(value=”/product?”):可匹配“/product1”或“/producta”,但不匹配“/product”或“/productaa”; 
    @RequestMapping(value=”/product*”):可匹配“/productabc”或“/product”,但不匹配“/productabc/abc”; 
    @RequestMapping(value=”/product/*”):可匹配“/product/abc”,但不匹配“/productabc”; 
    @RequestMapping(value=”/products/**/{productId}”):可匹配“/products/abc/abc/123”或“/products/123”,也就是Ant风格和URI模板变量风格可混用;
  • 正则表达式风格的URL路径映射 
    从Spring3.0开始支持正则表达式风格的URL路径映射,格式为{变量名:正则表达式},这样我们就可以通过@PathVariable提取模式中的{×××:正则表达式匹配的值}中的×××变量了。 
    @RequestMapping(value=”/products/{categoryCode:\d+}-{pageNumber:\d+}”):可以匹配“/products/123-1”,但不能匹配“/products/abc-1”,这样可以设计更加严格的规则。

    到此,我们学习了spring Web MVC提供的强大的URL路径映射,而且可以实现非常复杂的URL规则。Spring Web MVC不仅仅提供URL路径映射,还提供了其他强大的映射规则。接下来我们看一下请求方法映射限定吧。

请求方法映射限定

我们熟知的,展示表单一般为GET请求方法;提交表单一般为POST请求方法。但6.5.1节讲的URL路径映射方式对任意请求方法是全盘接受的,因此我们需要某种方式来告诉相应的功能处理方法只处理如GET请求方法的请求或POST请求方法的请求。

package cn.javass.chapter6.web.controller.method;//省略import@Controller@RequestMapping("/customers/**")                                     //①处理器的通用映射前缀public class RequestMethodController {    @RequestMapping(value="/create", method = RequestMethod.GET)//②类级别的@RequestMapping窄化    public String showForm() {        System.out.println("===============GET");        return "customer/create";      }    @RequestMapping(value="/create", method = RequestMethod.POST)//③类级别的@RequestMapping窄化    public String submit() {        System.out.println("================POST");        return "redirect:/success";            }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 组合使用是“或”的关系

@RequestMapping(value=”/methodOr”, method = {RequestMethod.POST, RequestMethod.GET}):即请求方法可以是 GET 或 POST。

请求参数数据映射限定

  • 请求数据中有指定参数名
package cn.javass.chapter6.web.controller.parameter;//省略import@Controller@RequestMapping("/parameter1")                                      //①处理器的通用映射前缀public class RequestParameterController1 {    //②进行类级别的@RequestMapping窄化    @RequestMapping(params="create", method=RequestMethod.GET)     public String showForm() {        System.out.println("===============showForm");        return "parameter/create";            }    //③进行类级别的@RequestMapping窄化    @RequestMapping(params="create", method=RequestMethod.POST)      public String submit() {        System.out.println("================submit");        return "redirect:/success";            }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

②@RequestMapping(params=”create”, method=RequestMethod.GET) :表示请求中有“create”的参数名且请求方法为“GET”即可匹配,如可匹配的请求URL“http://×××/parameter1?create”; 
③@RequestMapping(params=”create”, method=RequestMethod.POST):表示请求中有“create”的参数名且请求方法为“POST”即可匹配;

此处的create请求参数名表示你请求的动作,即你想要的功能的一个标识,常见的CRUD(增删改查)我们可以使用如下请求参数名来表达:

  • 请求数据中没有指定参数名
//请求参数不包含 create参数名@RequestMapping(params="!create", method=RequestMethod.GET)//进行类级别的@RequestMapping窄化
  • 1
  • 2
  • 3

@RequestMapping(params=”!create”, method=RequestMethod.GET):表示请求中没有“create”参数名且请求方法为“GET”即可匹配,如可匹配的请求URL“http://×××/parameter1?abc”。

  • 请求数据中指定参数名=值
package cn.javass.chapter6.web.controller.parameter;//省略import@Controller@RequestMapping("/parameter2")                      //①处理器的通用映射前缀public class RequestParameterController2 {    //②进行类级别的@RequestMapping窄化    @RequestMapping(params="submitFlag=create", method=RequestMethod.GET)      public String showForm() {        System.out.println("===============showForm");        return "parameter/create";            }    //③进行类级别的@RequestMapping窄化    @RequestMapping(params="submitFlag=create", method=RequestMethod.POST)       public String submit() {        System.out.println("===============submit");        return "redirect:/success";            }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

②@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”即可匹配;

此处的submitFlag=create请求参数表示你请求的动作,即你想要的功能的一个标识,常见的CRUD(增删改查)我们可以使用如下请求参数名来表达:

  • 请求数据中指定参数名!=值
//请求参数submitFlag 不等于 create@RequestMapping(params="submitFlag!=create", method=RequestMethod.GET)  
  • 1
  • 2
  • 3

@RequestMapping(params=”submitFlag!=create”, method=RequestMethod.GET):表示请求中的参数“submitFlag!=create”且请求方法为“GET”即可匹配,如可匹配的请求URL“http://×××/parameter1?submitFlag=abc”。

  • 组合使用是“且”的关系
@RequestMapping(params={"test1", "test2=create"})  //②进行类级别的@RequestMapping窄化
  • 1

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

请求头数据映射限定

  • 请求头数据中有指定参数名 
    @RequestMapping(value=”/header/test1”, headers = “Accept”):表示请求的URL必须为“/header/test1” 
    且 请求头中必须有Accept参数才能匹配。
  • 请求头数据中没有指定参数名 
    @RequestMapping(value=”/header/test2”, headers = “!abc”):表示请求的URL必须为“/header/test2” 
    且 请求头中必须没有abc参数才能匹配。(将Modify Header的abc参数值删除即可)。
  • 请求头数据中指定参数名=值 
    @RequestMapping(value=”/header/test3”, headers = “Content-Type=application/json”):表示请求的URL必须为“/header/test3” 且 请求头中必须有“Content-Type=application/json”参数即可匹配。(将Modify Header的Content-Type参数值改为“application/json”即可); 
    当你请求的URL为“/header/test3” 但 如果请求头中没有或不是“Content-Type=application/json”参数(如“text/html”其他参数),将返回“HTTP Status 415”状态码【表示不支持的媒体类型(Media Type),也就是MIME类型】,即我们的功能处理方法只能处理application/json的媒体类型。

@RequestMapping(value=”/header/test5”, headers = “Accept=text/*”) :表示请求的URL必须为“/header/test5” 且 请求头中必须有如“Accept=text/plain”参数即可匹配。(将Modify Header的Accept参数值改为“text/plain”即可);

Accept=text/*:表示主类型为text,子类型任意,如“text/plain”、“text/html”等都可以匹配。

阅读全文
0 0
原创粉丝点击