Home > Java > javaTutorial > body text

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

黄舟
Release: 2017-03-03 10:49:58
Original
1366 people have browsed it

In series (SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action) we showed a simple get request and returned a simple helloworld page. In this article, we will learn how to configure the URL mapping rules of an action.

In series (SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action) we configured a @RequestMapping(value = "/helloworld") on HelloWorldController, which means that all action requests to the controller must start with "/helloworld".

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.URL path mapping

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.Configure multiple URL mappings for one action:

We put @ in the index() action method of HelloWorldController in the previous article RequestMapping is changed to @RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET}), which means that the action is configured with two mappings of /index and /hello. Run the test as follows:

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action

#You can see that the /helloworld/hello request is also successfully matched.

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action. URL request parameter mapping:

This is often used when querying, for example, we obtain a certain record based on id or number.

Add a getDetail action in HelloWorldController, the code is as follows:

@RequestMapping(value="/detail/{id}", method = {RequestMethod.GET})public ModelAndView getDetail(@PathVariable(value="id") Integer id){
    
    ModelAndView modelAndView = new ModelAndView();  
    modelAndView.addObject("id", id);  
    modelAndView.setViewName("detail");  
    return modelAndView;
}
Copy after login


where value="/detail/{id}", {id} in It is a placeholder indicating that the URL requested as /detail/xxxx can be mapped, such as: /detail/SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (3) Mapping Rules from URL Request to Action, etc.

Method parameter @PathVariable(value="id") Integer id is used to map the variable corresponding to the placeholder in the URL to the parameter id, the value of value in @PathVariable(value="id") Must be consistent with the value in the placeholder /{id} braces.

Add the detail.jsp view in views to display the obtained id value. The view content is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"
    pageEncoding="UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"%><!DOCTYPE html PUBLIC "-//WSpringMVC Learning Series (3) Mapping Rules from URL Request to ActionC//DTD HTML SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.0SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action Transitional//EN" "http://www.wSpringMVC Learning Series (3) Mapping Rules from URL Request to Action.org/TR/htmlSpringMVC Learning Series (3) Mapping Rules from URL Request to Action/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"><title>Insert title here</title></head><body>
    ${id}</body></html>
Copy after login


Run the test, Request URL addresshttp://localhost:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/detail/SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (3) Mapping Rules from URL Request to Action, the results are as follows :

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action

You can see that the id we requested has been correctly displayed.

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.URL wildcard mapping:

We can also configure URL mapping through wildcard characters. The wildcard characters include "?" and "*". Among them, "?" means SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action character, "*" means matching multiple characters, and "**" means matching 0 or more paths.

For example:

"/helloworld/index?" can match "/helloworld/indexA" and "/helloworld/indexB", but cannot match "/helloworld/index" nor " /helloworld/indexAA";

"/helloworld/index*" can match "/helloworld/index", "/helloworld/indexA", "/helloworld/indexAA" but not "/helloworld/index/ A";

"/helloworld/index/*" can match "/helloworld/index/", "/helloworld/index/A", "/helloworld/index/AA", "/helloworld/index" /AB" but cannot match "/helloworld/index", "/helloworld/index/A/B";

"/helloworld/index/**" can match "/helloworld/index/" There are many sub-paths, such as: "/helloworld/index/A/B/C/D";

If there are now "/helloworld/index" and "/helloworld/*", if the request address is " /helloworld/index" So how will it match? Spring MVC will match based on the longest match first principle (that is, which one matches the most in the mapping configuration), so it will match "/helloworld/index". Let's do the test below:

Add a urlTest in HelloWorldController action, the content is as follows:

@RequestMapping(value="/*", method = {RequestMethod.GET})public ModelAndView urlTest(){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.setViewName("urltest");  
    return modelAndView;
}
Copy after login


Add a new view urltest.jsp in the views folder. In order to distinguish it from index.jsp, the content of urltest.jsp is as follows:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"
    pageEncoding="UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"%><!DOCTYPE html PUBLIC "-//WSpringMVC Learning Series (3) Mapping Rules from URL Request to ActionC//DTD HTML SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.0SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action Transitional//EN" 
    "http://www.wSpringMVC Learning Series (3) Mapping Rules from URL Request to Action.org/TR/htmlSpringMVC Learning Series (3) Mapping Rules from URL Request to Action/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action">
    <title>Insert title here</title>
    </head>
    <body>
    urlTest!</body></html>
Copy after login


Request http://localhost:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/index to view the results:

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

You can see the mapping is the action corresponding to index.

Request http://localhost:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/AAA to view the results:

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

It can be seen that the action corresponding to urlTest is mapped.

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.URL regular expression mapping:

Spring MVC还支持正则表达式方式的映射配置,我们通过一个测试来展示:

在HelloWorldController添加一个regUrlTest的action,内容如下:

@RequestMapping(value="/reg/{name:\\w+}-{age:\\d+}", 
method = {RequestMethod.GET})public ModelAndView regUrlTest(@PathVariable(value="name") String name, 
@PathVariable(value="age") Integer age){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.addObject("name", name); 
    modelAndView.addObject("age", age); 
    modelAndView.setViewName("regurltest");  
    return modelAndView;
}
Copy after login


在views文件夹中新加一个视图regurltest.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"
    pageEncoding="UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"%><!DOCTYPE html PUBLIC "-//WSpringMVC Learning Series (3) Mapping Rules from URL Request to ActionC//DTD HTML SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.0SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action Transitional//EN" 
    "http://www.wSpringMVC Learning Series (3) Mapping Rules from URL Request to Action.org/TR/htmlSpringMVC Learning Series (3) Mapping Rules from URL Request to Action/loose.dtd"><html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action">
    <title>Insert title here</title></head><body>
    ${name}-${age}</body></html>
Copy after login


请求http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/reg/Hanmeimei-SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (3) Mapping Rules from URL Request to Action查看结果:

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

请求http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei查看结果(会发现找不到对应的action返回SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action):

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.限制action所接受的请求方式(get或post):

之前我们在HelloWorldController的index() action方法上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET})我们把它改为@RequestMapping(value="/*", method = {RequestMethod.POST})再次请求http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/index试一下:

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

这里可以看到结果映射到了urlTest这个action,这是因为我们在urlTest上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET}),当index这个action映射不在符合时便映射到了urlTest。

我们也可以这样配置@RequestMapping(value="/*", method = {RequestMethod.GET, RequestMethod.POST})表示该action可以接受get或post请求,不过更简单的是不对method做配置则默认支持所有请求方式。

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.限制action所接受请求的参数:

我们可以为某个action指定映射的请求中必须包含某参数,或必须不包含某参数,或者某参数必须等于某个值,或者某参数必须不等于某个值这些限制。

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.指定映射请求必须包含某参数:

在HelloWorldController添加一个paramsTest的action,内容如下:

@RequestMapping(value="/paramstest", params="example", 
method = {RequestMethod.GET})public ModelAndView paramsTest(){
    
    ModelAndView modelAndView = new ModelAndView();   
    modelAndView.setViewName("paramstest");  
    return modelAndView;
}
Copy after login


在views文件夹中新加一个视图paramstest.jsp内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"
    pageEncoding="UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"%><!DOCTYPE html PUBLIC "-//WSpringMVC Learning Series (3) Mapping Rules from URL Request to ActionC//DTD HTML SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.0SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action Transitional//EN" 
    "http://www.wSpringMVC Learning Series (3) Mapping Rules from URL Request to Action.org/TR/htmlSpringMVC Learning Series (3) Mapping Rules from URL Request to Action/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; 
    charset=UTF-SpringMVC Learning Series (3) Mapping Rules from URL Request to Action"><title>Insert title here</title></head><body>
    paramstest!</body></html>
Copy after login


请求http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest查看结果:

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

这里可以看到没有找到paramsTest这个action结果还是映射到了urlTest这个action。

请求http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest?example查看结果:

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action

这次可以看到请求映射到了paramsTest这个action。

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action.指定映射请求必须包含某参数:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="!example", method = {RequestMethod.GET})

重新请求http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest?example查看结果:

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action0

可以看到又没有找到paramsTest这个action而映射到了urlTest这个action。

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.指定映射请求中或者某参数必须等于某个值:

把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="example=AAA", method = {RequestMethod.GET})

Request http://www.php.cn/:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest?example=BBB View results:

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action

You can see that paramsTest was not found This action is mapped to the urlTest action.

Request http://localhost:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest?example=BBB View the results:

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action

This time you can see that the request is mapped to paramsTest this action.

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (3) Mapping Rules from URL Request to Action. Specify that a certain parameter in the mapping request must not be equal to a certain value:

Change the @RequestMapping(value="/paramstest", of the paramsTest you just added, params="example", method = {RequestMethod.GET}) changed to @RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})

Request http://localhost:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest?example=AAA View the results:

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (3) Mapping Rules from URL Request to Action

You can see The request is mapped to the action paramsTest.

Request http://localhost:SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0SpringMVC Learning Series (3) Mapping Rules from URL Request to Action0/SpringMVCLesson/helloworld/paramstest?example=AAA View the results:

SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to ActionSpringMVC Learning Series (3) Mapping Rules from URL Request to Action

##You can see that the paramsTest action was not found And it is mapped to the action urlTest.

Note: When we specify multiple parameters for params, such as: params={"exampleSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action", "exampleSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action"}, it represents an and relationship, that is, the two parameter restrictions must be met at the same time.

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action. Limit the request header parameters accepted by action:

Same as limiting the request parameters accepted by action, we can also specify mapping for an action The request header must contain a certain parameter, or must not contain a certain parameter, or a certain parameter must be equal to a certain value, or a certain parameter must not be equal to a certain value.

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action. Specify that the mapping request header must contain certain parameters:

@RequestMapping(value="/headerTest", headers = "example"). It is the same as limiting request parameters. You can refer to the above example for testing.

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action. The specified mapping request header must

not contain certain parameters:

@RequestMapping(value="/headerTest", headers = "!example"). It is the same as limiting request parameters. You can refer to the above example for testing.

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (3) Mapping Rules from URL Request to Action. Specify that a certain parameter in the mapping request header must be equal to a certain value:

@RequestMapping(value="/headerTest", headers = "Accept=text/html"). It is the same as limiting request parameters. You can refer to the above example for testing.

SpringMVC Learning Series (3) Mapping Rules from URL Request to Action.SpringMVC Learning Series (3) Mapping Rules from URL Request to Action. Specify that a certain parameter in the mapping request header must

not be equal to a certain value:

@RequestMapping(value="/headerTest", headers = "Accept! =text/html"). It is the same as limiting request parameters. You can refer to the above example for testing.

Note: When we specify multiple parameters for headers, such as: headers={"exampleSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action", "exampleSpringMVC Learning Series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action) Mapping Rules from URL Request to Action"}, it represents an and relationship, that is, the two parameter restrictions must be met at the same time.

The above is the content of the mapping rules from URL request to Action in SpringMVC learning series (SpringMVC Learning Series (3) Mapping Rules from URL Request to Action), more related content Please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template