java spring mvc requestmapping动态url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27574537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring mvc requestmapping dynamic url
提问by user3795951
How do I change my request mapping for dynamic urls? URLs might look like this:
如何更改动态 url 的请求映射?URL 可能如下所示:
- http://zz.zz.zz.com:8080/webapp/p1/q9/e3/test?Id=2&maxrows=5
- http://zz.zz.zz.com:8080/webapp/a1/b2/c3/test?Id=2&maxrows=5
- http://zz.zz.zz.com:8080/webapp/x1/y2/z3/test?Id=2&maxrows=5
- http://zz.zz.zz.com:8080/webapp/p1/q9/e3/test?Id=2&maxrows=5
- http://zz.zz.zz.com:8080/webapp/a1/b2/c3/test?Id=2&maxrows=5
- http://zz.zz.zz.com:8080/webapp/x1/y2/z3/test?Id=2&maxrows=5
Here's the working controller syntax when the url is in this format:
这是当 url 为这种格式时的工作控制器语法:
http://zz.zz.zz.com:8080/webapp/test?Id=2&maxrows=5
http://zz.zz.zz.com:8080/webapp/test?Id=2&maxrows=5
@RequestMapping(value = "/test", method = RequestMethod.GET)
public @ResponseBody void test(
@RequestParam(value = "Id", required = true) String Id,
@RequestParam(value = "maxrows", required = true) int maxrows
) throws Exception {
System.out.println("Id: " + Id + " maxrows: " + maxrows);
}
回答by Lukas Risko
Try this:
试试这个:
@RequestMapping(value = "/test/{param1}/{param2}/{param3}")
public @ResponseBody void test(
@RequestParam(value = "Id", required = true) String Id,
@RequestParam(value = "maxrows", required = true) int maxrows,
@PathVariable(value = "param1") String param1,
@PathVariable(value = "param2") String param2,
@PathVariable(value = "param3") String param3) {
...
}
For more information look at Spring Reference Documentation
有关更多信息,请查看Spring 参考文档