@Controller @RequestMapping("/user") //1处理器的通用映射前缀 public class HelloWorldController2 { @RequestMapping("/hello2") //2相对于1处的映射进行窄化 public ModelAndView helloWorld() { System.out.print("hello!"); ..如下省略 } }
此时访问localhost:8888/demo/hello2 在控制台内是没法打印出“hello!”的java
值能访问localhost:8888/demo/user/hello2 才能访问次方法,堪称方法级别的RequestMappingweb
同时也叫窄化请求映射正则表达式
@Controller public class HelloWorldController2 { @RequestMapping("/hello2","/testhello") //多URL映射同一处理方法 public ModelAndView helloWorld() { System.out.print("hello!"); ..如下省略 } }
@Controller public class HelloWorldController2 { @RequestMapping("/hello2/{fid}") //fid是占位符 public ModelAndView helloWorld() { System.out.print("hello!"); ..如下省略 } }
经过这种占位符的方式能够经过/hello2/123444或者/hello/abc的映射访问该处理器,经过@PathVariable能够提取URI模板模式中的{×××}中的×××变量。固然修改/hello2/{fid}为/hello2/{fid}/test同理浏览器
@RequestMapping("/users/**"):能够匹配“/users/abc/abc”,但“/users/123”将会被【URI模板模式映射中的“/users/{userId}”模式优先映射到session
@RequestMapping("/product?"):可匹配“/product1”或“/producta”,但不匹配“/product”或“/productaa”;app
@RequestMapping("/product*"):可匹配“/productabc”或“/product”,但不匹配“/productabc/abc”;测试
@RequestMapping("/product/*"):可匹配“/product/abc”,但不匹配“/productabc”;ui
@RequestMapping("/products/**/{productId}"):可匹配“/products/abc/abc/123”或“/products/123”url
也就是Ant风格和URI模板变量风格可混用;spa
使用@PathVariable取值时,必须使用基本类型定义变量
@Controller public class Test { @RequestMapping("test/{fid}") public void test1(@PathVariable("fid") String fid, @RequestParam("a")String a,HttpServletResponse res) throws IOException{ System.out.println(fid); System.out.println(a); res.getWriter().print(a); } }
此时访问http://localhost:8888/SpringMVCTest/test/12334?a=333
在控制台会打印出12334和333两个参数
但若是访问http://localhost:8888/SpringMVCTest/test?a=333
则会报404错误
@RequestMapping(value="/products/{categoryCode:\\d+}-{pageNumber:\\d+}"):能够匹配“/products/123-1”,但不能匹配“/products/abc-1”,这样能够设计更加严格的规则。
@Controller public class Test { @RequestMapping(value="test/{fid}",method = RequestMethod.POST) public void test1(@PathVariable("fid") String fid, @RequestParam("a")String a,HttpServletResponse res) throws IOException{ System.out.println(fid); System.out.println(a); res.getWriter().print(a); } }
若是此时直接经过浏览器访问http://localhost:8888/SpringMVCTest/test/12334?a=333则会报
HTTP Status 405 - Request method 'GET' not supported
错误,必须使用POST请求才能指定到该映射,而将
method = RequestMethod.POST
改成
method = RequestMethod.GET
便可直接经过网页访问
注意:此时由于@requestMapping内存在多个参数,
@RequestMapping(value="test/{fid}",method = RequestMethod.POST)
因此不可再简写为
@RequestMapping("test/{fid}")
除了GET、POST,还有HEAD、OPTIONS、PUT、DELETE、TRACE。
DispatcherServlet默认开启对 GET、POST、PUT、DELETE、HEAD的支持
若是须要支持OPTIONS、TRACE,请添加DispatcherServlet在web.xml的初始化参数:dispatchOptionsRequest 和 dispatchTraceRequest 为true。
@Controller public class Test { @RequestMapping(value="test/{fid}",method = RequestMethod.GET,params="dai") public void test1(@PathVariable("fid") String fid, @RequestParam("a")String a,HttpServletResponse res) throws IOException{ System.out.println(fid); System.out.println(a); res.getWriter().print(a); } }
此时经过访问:http://localhost:8888/SpringMVCTest/test/12334?a=333 会出现404错误
须要经过访问:http://localhost:8888/SpringMVCTest/test/12334?dai&a=333正常
若是将
params="dai"
改成
params="!dai" 访问时参数名不带dai便可访问
访问时参数名dai的值为123时可访问 params="dai=123"
访问时参数名dai的值不为123时可访问 params="dai!=123"
如下信息来源:http://jinnianshilongnian.iteye.com/blog/1705701
参数:
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required:是否必须,默认是true,表示请求中必定要有相应的参数,不然将报404错误码;
defaultValue:默认值,表示若是请求中没有同名参数时的默认值,默认值能够是SpEL表达式,如“#
若是请求中有多个同名的应该如何接收呢?如给用户受权时,可能授予多个权限,首先看下以下代码:
public String test(@RequestParam(value="role") String roleList)
若是请求参数相似于url?role=admin&role=user,则实际roleList参数入参的数据为“admin,user”,即多个数据之间使用“,”分割;咱们应该使用以下方式来接收多个请求参数:
public String test(@RequestParam(value="role") String[] roleList)
或者
public String test(@RequestParam(value="list") List<String> list)
@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。
@RequestMapping(value="/users/{userId}/topics/{topicId}") public String test( @PathVariable(value="userId") int userId, @PathVariable(value="topicId") int topicId)
如请求的URL为“控制器URL/users/123/topics/456”,则自动将URL中模板变量{userId}和{topicId}绑定到经过@PathVariable注解的同名参数上,即入参后userId=12三、topicId=456。代码在PathVariableTypeController中。
@CookieValue用于将请求的Cookie数据映射到功能处理方法的参数上。
public String test(@CookieValue(value="JSESSIONID", defaultValue="") String sessionId)
如上配置将自动将JSESSIONID值入参到sessionId参数上,defaultValue表示Cookie中没有JSESSIONID时默认为空。
传入参数类型也能够是javax.servlet.http.Cookie类型。
测试代码在CookieValueTypeController中。@CookieValue也拥有和@RequestParam相同的三个参数,含义同样。
@RequestHeader用于将请求的头信息区数据映射到功能处理方法的参数上。
@RequestMapping(value="/header") public String test( @RequestHeader("User-Agent") String userAgent, @RequestHeader(value="Accept") String[] accepts)
如上配置将自动将请求头“User-Agent”值入参到userAgent参数上,并将“Accept”请求头值入参到accepts参数上。测试代码在HeaderValueTypeController中。
@RequestHeader也拥有和@RequestParam相同的三个参数,含义同样。
@ModelAttribute一个具备以下三个做用:
①绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,并且自动暴露为模型数据用于视图页面展现时使用;
②暴露表单引用对象为模型数据:放在处理器的通常方法(非功能处理方法)上时,是为表单准备要展现的表单引用对象,如注册时须要选择的所在城市等,并且在执行功能处理方法(@RequestMapping注解的方法)以前,自动添加到模型对象中,用于视图页面展现时使用;
③暴露@RequestMapping方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展现时使用。
有时候咱们须要在屡次请求之间保持数据,通常状况须要咱们明确的调用HttpSession的API来存取会话数据,如多步骤提交的表单。Spring Web MVC提供了@SessionAttributes进行请求间透明的存取会话数据。
七、@RequestBody绑定请求的内容区数据并能进行自动类型转换等。
八、@RequestPart绑定“multipart/data”数据,除了能绑定@RequestParam能作到的请求参数外,还能绑定上传的文件等。