SpringMvc中,RequestMethod可以同时支持占中的明星很惨POST GET访问么

@PathVariable和@RequestParam的区别
请求路径上有个id的变量值,可以通过@PathVariable来获取 @RequestMapping(value = "/page/{id}", method = RequestMethod.GET) @RequestParam用来获得静态的URL请求入参
spring注解时action里用到。
handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型)
A、处理requet uri 部分(这里指uri
template中variable,不含queryString部分)的注解:
请求路径上有个id的变量值,可以通过@PathVariable来获取
@RequestMapping(value = "/page/{id}",
method = RequestMethod.GET)
@RequestParam用来获得静态的URL请求入参
spring注解时action里用到。
handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型)
A、处理requet uri 部分(这里指uri
template中variable,不含queryString部分)的注解:
B、处理request header部分的注解:
@RequestHeader,
C、处理request body部分的注解:@RequestParam,
D、处理attribute类型是注解: @SessionAttributes,
1、 @PathVariable
当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过
@Pathvariable注解绑定它传过来的值到方法的参数上。
示例代码:
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController {
@RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
上面代码把URI template 中变量
ownerId的值和petId的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri
template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。
2、 @RequestHeader、@CookieValue
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。
示例代码:
这是一个Request 的header部分:
localhost:8080
text/html,application/xhtml+xml,application/q=0.9
Accept-Language
fr,en-q=0.7,q=0.3
Accept-Encoding
gzip,deflate
Accept-Charset
ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive)
上面的代码,把request header部分的 Accept-Encoding的值,绑定到参数encoding上了,
Keep-Alive header的值绑定到参数keepAlive上。
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上。
例如有如下Cookie值:
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
参数绑定的代码:
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie)
即把JSESSIONID的值绑定到参数cookie上。
3、@RequestParam, @RequestBody
@RequestParam
A) 常用来处理简单类型的绑定,通过Request.getParameter()
获取的String可直接转换为简单类型的情况( String--&
简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get
方式中queryString的值,也可以处理post方式中 body data的值;
B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST;
C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
示例代码:
@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
@RequestBody
该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json,
application/xml等;
它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data
body,然后绑定到相应的bean上的。
因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap&String,
String&里,这种情况在某些特殊需求下使用,详情查看FormHttpMessageC
示例代码:
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
4、@SessionAttributes, @ModelAttribute
@SessionAttributes:
该注解用来绑定HttpSession中的attribute对象的值,便于在方法中的参数里使用。
该注解有value、types两个属性,可以通过名字和类型指定要使用的attribute 对象;
示例代码:
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {
@ModelAttribute
该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时:
通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于:
A) @SessionAttributes 启用的attribute 对象上;
B) @ModelAttribute 用于方法上时指定的model对象;
C) 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。
用到方法上@ModelAttribute的示例代码:
// Add one attribute
// The return value of the method is added to the model under the name "account"
// You can customize the name via @ModelAttribute("myAccount")
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
这种方式实际的效果就是在调用@RequestMapping的方法之前,为request对象的model里put(&account&,
Account);
用在参数上的@ModelAttribute示例代码:
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
@SessionAttributes有无绑定的Pet对象,若没有则查询@ModelAttribute方法层面上是否绑定了Pet对象,若没有则将URI
template中的值按对应的名称绑定到Pet对象的各属性上。
本文转自:苦逼的青春http://csjava./blog/static/8/?COLLCC=&COLLCC=&COLLCC=&
阅读(...) 评论()想的太多,做的太少,中间的落差就是烦恼,要么去做,要么别想...
在系列(2)中我们展示了一个简单的get请求,并返回了一个简单的helloworld页面。本篇我们来学习如何来配置一个action的url映射规则。
在系列(2)中我们在HelloWorldController上配置了一个@RequestMapping(value = "/helloworld")这表示对该controller的所有action请求必须是以"/helloworld&开始。
1.URL路径映射
1.1.对一个action配置多个URL映射:
我们把上一篇中的HelloWorldController的index() action方法的@RequestMapping更改为@RequestMapping(value={"/index", "/hello"}, method = {RequestMethod.GET}),这表示对该action配置了/index和/hello两个映射。运行测试,如下:
可以看到/helloworld/hello请求也成功匹配。
1.2.URL请求参数映射:
这在查询的时候经常用到,比如我们根据id或编号来获取某一条记录。
在HelloWorldController添加一个getDetail的action,代码如下:
@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 modelAndV
其中value="/detail/{id}",中的{id}为占位符表示可以映射请求为/detail/xxxx 的URL如:/detail/123等。
方法的参数@PathVariable(value="id") Integer id 用于将URL中占位符所对应变量映射到参数id上,@PathVariable(value="id") 中value的值要和占位符/{id}大括号中的值一致。
在views中添加detail.jsp视图,用于将获取到的id值展示出来。视图内容如下:
&%@ page language="java" contentType="text/ charset=UTF-8"
pageEncoding="UTF-8"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&Insert title here&/title&
运行测试,请求URL地址 http://localhost:8080/SpringMVCLesson/helloworld/detail/123 ,结果如下:
可以看到已经正确的显示了我们请求的id。
1.3.URL通配符映射:
我们还可以通过通配符对URL映射进行配置,通配符有&?&和&*&两个字符。其中&?&表示1个字符,&*&表示匹配多个字符,&**&表示匹配0个或多个路径。
&/helloworld/index?&可以匹配&/helloworld/indexA&、&/helloworld/indexB&,但不能匹配&/helloworld/index&也不能匹配&/helloworld/indexAA&;
&/helloworld/index*&可以匹配&/helloworld/index&、&/helloworld/indexA&、&/helloworld/indexAA&但不能匹配&/helloworld/index/A&;
&/helloworld/index/*&可以匹配&/helloworld/index/&、&/helloworld/index/A&、&/helloworld/index/AA&、&/helloworld/index/AB&但不能匹配&&& &/helloworld/index&、&/helloworld/index/A/B&;
&/helloworld/index/**&可以匹配&/helloworld/index/&下的多有子路径,比如:&/helloworld/index/A/B/C/D&;
如果现在有&/helloworld/index&和&/helloworld/*&,如果请求地址为&/helloworld/index&那么将如何匹配?Spring MVC会按照最长匹配优先原则(即和映射配置中哪个匹配的最多)来匹配,所以会匹配&/helloworld/index&,下面来做测试:
在HelloWorldController添加一个urlTest的action,内容如下:
@RequestMapping(value="/*", method = {RequestMethod.GET})
public ModelAndView urlTest(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("urltest");
return modelAndV
在views文件夹中新加一个视图urltest.jsp,为了和index.jsp做区别urltest.jsp的内容如下:
&%@ page language="java" contentType="text/ charset=UTF-8"
pageEncoding="UTF-8"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&Insert title here&/title&
请求http://localhost:8080/SpringMVCLesson/helloworld/index查看结果:
可以看出映射的是index对应的action。
请求http://localhost:8080/SpringMVCLesson/helloworld/AAA查看结果:
可以看出映射的是urlTest对应的action。
1.4.URL正则表达式映射:
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 modelAndV
在views文件夹中新加一个视图regurltest.jsp内容如下:
&%@ page language="java" contentType="text/ charset=UTF-8"
pageEncoding="UTF-8"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&Insert title here&/title&
${name}-${age}
请求http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-18查看结果:
请求http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei查看结果(会发现找不到对应的action返回404):
2.限制action所接受的请求方式(get或post):
之前我们在HelloWorldController的index() action方法上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET})我们把它改为@RequestMapping(value="/*", method = {RequestMethod.POST})再次请求http://localhost:8080/SpringMVCLesson/helloworld/index试一下:
这里可以看到结果映射到了urlTest这个action,这是因为我们在urlTest上配置的为@RequestMapping(value="/*", method = {RequestMethod.GET}),当index这个action映射不在符合时便映射到了urlTest。
我们也可以这样配置@RequestMapping(value="/*", method = {RequestMethod.GET, RequestMethod.POST})表示该action可以接受get或post请求,不过更简单的是不对method做配置则默认支持所有请求方式。
3.限制action所接受请求的参数:
我们可以为某个action指定映射的请求中必须包含某参数,或必须不包含某参数,或者某参数必须等于某个值,或者某参数必须不等于某个值这些限制。
3.1.指定映射请求必须包含某参数:
在HelloWorldController添加一个paramsTest的action,内容如下:
@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET})
public ModelAndView paramsTest(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("paramstest");
return modelAndV
在views文件夹中新加一个视图paramstest.jsp内容如下:
&%@ page language="java" contentType="text/ charset=UTF-8"
pageEncoding="UTF-8"%&
&!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&
&meta http-equiv="Content-Type" content="text/ charset=UTF-8"&
&title&Insert title here&/title&
paramstest!
请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest查看结果:
这里可以看到没有找到paramsTest这个action结果还是映射到了urlTest这个action。
请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example查看结果:
这次可以看到请求映射到了paramsTest这个action。
3.2.指定映射请求必须不包含某参数:
把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="!example", method = {RequestMethod.GET})
重新请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example查看结果:
可以看到又没有找到paramsTest这个action而映射到了urlTest这个action。
3.3.指定映射请求中或者某参数必须等于某个值:
把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="example=AAA", method = {RequestMethod.GET})
请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看结果:
可以看到没有找到paramsTest这个action而映射到了urlTest这个action。
请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA查看结果:
这次可以看到请求映射到了paramsTest这个action。
3.4.指定映射请求中或者某参数必须不等于某个值:
把刚才添加的paramsTest的@RequestMapping(value="/paramstest", params="example", method = {RequestMethod.GET}) 改为@RequestMapping(value="/paramstest", params="example!=AAA", method = {RequestMethod.GET})
请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB查看结果:
可以看到请求映射到了paramsTest这个action。
请求http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA查看结果:
可以看到没有找到paramsTest这个action而映射到了urlTest这个action。
注:当我们为params指定多个参数时如:params={"example1", "example2"},表示的是and关系,即两个参数限制必须同时满足。
4.限制action所接受请求头参数:
同限制action所接受的请求参数一样,我们也可以为某个action指定映射的请求头中必须包含某参数,或必须不包含某参数,或者某参数必须等于某个值,或者某参数必须不等于某个值这些限制。
4.1.指定映射请求头必须包含某参数:
@RequestMapping(value="/headerTest", headers = "example")。与限制请求参数是一样的,可以参考上面的例子进行测试。
4.2.指定映射请求头必须不包含某参数:
@RequestMapping(value="/headerTest", headers = "!example")。与限制请求参数是一样的,可以参考上面的例子进行测试。
4.3.指定映射请求头中或者某参数必须等于某个值:
@RequestMapping(value="/headerTest", headers = "Accept=text/html")。与限制请求参数是一样的,可以参考上面的例子进行测试。
4.4.指定映射请求头中或者某参数必须不等于某个值:
@RequestMapping(value="/headerTest", headers = "Accept!=text/html")。与限制请求参数是一样的,可以参考上面的例子进行测试。
注:当我们为headers指定多个参数时如:headers={"example1", "example2"},表示的是and关系,即两个参数限制必须同时满足。
代码下载:
注: 之前没注意前11篇的示例代码,不知道为什么当时打包上传上去的是没有.project项目文件的,导致下载后不能直接导入eclipse运行,虚拟机又 被我删掉了,这些示例代码也没有备份,但是代码文件还在的,所以可以新建一个Dynamic Web Project把对应的配置文件和controller还有view导入就可以了,给大家造成的不便说声抱歉。
阅读(...) 评论()2016年1月 Java大版内专家分月排行榜第二2015年12月 Java大版内专家分月排行榜第二2015年8月 Java大版内专家分月排行榜第二2015年3月 Java大版内专家分月排行榜第二2015年1月 Java大版内专家分月排行榜第二2014年12月 Java大版内专家分月排行榜第二2014年11月 Java大版内专家分月排行榜第二2014年6月 Java大版内专家分月排行榜第二2014年4月 Java大版内专家分月排行榜第二2014年1月 Java大版内专家分月排行榜第二2013年11月 Java大版内专家分月排行榜第二
2015年9月 Java大版内专家分月排行榜第三2015年6月 Java大版内专家分月排行榜第三2015年5月 Java大版内专家分月排行榜第三2015年2月 Java大版内专家分月排行榜第三2014年3月 Java大版内专家分月排行榜第三2013年12月 Java大版内专家分月排行榜第三
2013年10月 总版技术专家分月排行榜第三
2014年3月 Java大版内专家分月排行榜第一2014年1月 Java大版内专家分月排行榜第一2013年12月 Java大版内专家分月排行榜第一2013年11月 Java大版内专家分月排行榜第一2013年10月 Java大版内专家分月排行榜第一
2017年1月 总版技术专家分月排行榜第二
2016年12月 总版技术专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 中国支持云联惠上市 的文章

 

随机推荐