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nuPHcwZilK/1gxPxmpVwPtaaaT3EKttNK3jfbPztY3zHmK1jUck792Vr22Y9xCrbQLCeM8jX0vUlGXU5CHygeybk3Xtys7s9Am1szHLC7FaZxHDe075WrqmrBb/oNsrhm4ZO5mnbA1ZXojVugUxvOdUv1ZnGVVjmZpaMEO3ZnU8md4zM40Qq3UNYnjPqX5tzd6zcrE1eA+xWtcghvec8rVECJbaYNQWNwndsurXmqZsGVleiNW6BDG8x3zytaloBwoGDW9rGXKZvK81qV/LStmysrwQq3UNwniPOeRrmYOn+4BYbeMRyfu6EcR7iNU2HvAeMKEVY7UqnvIeAGTAe8CLgPeAFwHvAS8C3gNeBLwHvAh4D3gR8B7wIuA94EXAe8CLgPeAFwHvAS8C3gNeBLwHvAh4D3iR/wNl70YBsF9G7wAAAABJRU5ErkJggg==" alt="" />
1、@RequestMapping来映射URL
注解 @RequestMapping 可以用在类定义处和方法定义处。
类定义处:规定初步的请求映射,相对于web应用的根目录;
方法定义处:进一步细分请求映射,相对于类定义处的URL。如果类定义处没有使用该注解,则方法标记的URL相对于根目录而言;
package com.springmvc.helloworld_1;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping(value="/example")
public class HelloWorld { @RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world"); return "success";
}
}
上面代码在类定义处指定映射为"/example",在hello()方法处指定为"/helloworld"。那么hello()方法的URL映射地址为:http://localhost:8080/springMVC/example/helloworld
如果去掉类定义处的@RequestMapping(value="/example"),那么hello()方法的映射地址就变为了:http://localhost:8080/springMVC/helloworld
还有一个注意的,@RequestMapping的默认属性为value,所以@RequestMapping(value="/example")和@RequestMapping("/example")是等价的。
2、@RequestMapping除了可以指定URL映射外,还可以指定“请求方法、请求参数和请求头”的映射请求
注解的value、method、params及headers分别指定“请求的URL、请求方法、请求参数及请求头”。它们之间是与的关系,联合使用会使得请求的映射更加精细。
2.1 method属性可以指定请求的类型,http中规定请求有四种类型:get,post,put,delete。其值在枚举类型RequestMethod中有规定。
例如:@RequestMapping(value="/helloworld", method=RequestMethod.DELETE) 指定只有DELETE方式的helloworld请求才能够执行该处理方法。
2.2 params和headers支持简单的表达式:
—— params1:表示请求必须包含名为params1的请求参数
—— !params1:表示请求不能包含名为params1的请求参数
—— params1 != value1:表示请求必须包含名为params1的请求参数,但是其值不能是value1
—— {"params1 = value1", "param2"}:表示请求必须包含名为params1和params2两个请求参数,且params1的值必须为value1
2.3 Ant风格资源地址支持3种通配符:
—— ? : 匹配文件名中的一个字符
—— * : 匹配文件名中的任意多个字符(至少有一个字符)
—— ** : 匹配多层路径(至少有一层)
@RequestMapping支持Ant风格的URL:
—— /user/create?? 匹配/user/createAA、/user/createBB等URL (??匹配任意两个字符)
—— /user/*/createUser 匹配/user/aaa/createUser、/user/bbb/createUser等URL (*匹配任意字符)
—— /user/**/createUser 匹配/user/createUser、/user/aaa/bbb/createUser等URL (**匹配任意多层路径)
注意:其?和*必须要有,如果为空,则不符合
2.4 @PathVariable 映射URL绑定的占位符
可以在控制器处理方法的入参中使用 @PathVariable 获取到URL中占位符参数。 URL中的{xxx}占位符可以通过 @PathVariable("xxx") 绑定到操作方法的入参中。
@RequestMapping("/delete/{id}")
public String testPathVariable(@PathVariable("id") Integer id){
System.out.println("id = " + id); return SUCCESS;
}
下面是示例类将上面的知识点做了总结和应用示范:
package com.springmvc.RequestMapping_2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; @Controller
@RequestMapping("/springmvc")
public class RequestMappingTest { private static final String SUCCESS = "success"; /**
* 注解 @RequestMapping 可以用在类定义处和方法定义处
* 1、类定义处:规定初步的请求映射,相对于web应用的根目录
* 2、方法定义处:进一步细分请求映射,相对于类定义处的URL。如果类定义处没有使用该注解,则方法标记的URL相对于根目录而言
*
* 所以,testRequestMappingURL方法对应的URL目录为:/springmvc/testRequestMappingURL
*/
@RequestMapping("/testRequestMappingURL")
public String testRequestMappingURL(){
System.out.println("testRequestMappingURL 方法..."); return SUCCESS;
} /**
* 1、了解:可以指定params和headers参数。
*
* params和headers的值规定了:
* ①、请求参数必须包含param,和view。而且,view的值必须为true
* ②、请求头中必须包含有Accept-Language,而且其值必须为zh-CN,zh;q=0.8
*/
@RequestMapping(value="/testParamsAndHearders",
params={"view=true","param"},
headers={"Accept-Language=zh-CN,zh;q=0.8"})
public String testParamsAndHearders(){
System.out.println("testParamsAndHearders 方法..."); return SUCCESS;
} /**
* 2、Ant风格的占位符。
* —— ? : 匹配文件名中的一个字符
* —— * : 匹配文件名中的任意个字符(至少有一个)
* —— ** : 匹配多层路径(至少有一层)
*/
@RequestMapping(value="/*/testAnt??")
public String testAntPath(){
System.out.println("testAntPath 方法..."); return SUCCESS;
} /**
* 3、通过method指定请求方式必须是POST请求
*/
@RequestMapping(value="/testMethod", method=RequestMethod.POST)
public String testMethod(){
System.out.println("testMethod 方法..."); return SUCCESS;
} /**
* 4、可以使用注解@PathVariable("id")将@RequestMapping中的参数提取出来传递到方法的入参中
*/
@RequestMapping("/delete/{id}")
public String testPathVariable(@PathVariable("id") Integer id){
System.out.println("id = " + id); return SUCCESS;
}
}