Java 春天@RequestMapping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19627961/
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 @RequestMapping
提问by user2785929
I keep seeing this kind of param value = "/redirect/{id}"
in a @RequestMapping
annotation of the Spring. I keep wondering what is {id}
here? Is this some sort of Expression Language
?
我一直在 Springvalue = "/redirect/{id}"
的@RequestMapping
注释中看到这种参数。我一直想知道{id}
这里有什么?这是某种Expression Language
吗?
Sample code of what I have seen:
我所看到的示例代码:
@RequestMapping( value = "/files/{id}", method = RequestMethod.GET )
public void getFile( @PathVariable( "id" )
String fileName, HttpServletResponse response )
{
try
{
// get your file as InputStream
InputStream is = new FileInputStream("/pathToFile/"+ fileName);
// copy it to response's OutputStream
IOUtils.copy( is, response.getOutputStream() );
response.flushBuffer();
}
catch( IOException ex )
{
throw new RuntimeException( "IOError writing file to output stream" );
}
}
My question is what is the {id}
in the mapping and what is its relationship with the @PathVariable
annotation and how to use it? I red some info from the web but I will much more appreciate it to hear much more clearer explanation from you guys.
我的问题是{id}
映射中的 是什么,它与@PathVariable
注释的关系是什么以及如何使用它?我从网上红了一些信息,但我会更感激听到你们更清晰的解释。
采纳答案by Alex
The {foo}
part in a @RequestMapping
value is a path variable which means a value retrieved from the url path and not from a request parameter.
值中的{foo}
部分@RequestMapping
是路径变量,这意味着从 url 路径而不是从请求参数中检索的值。
For example if the user access to /files/foo.zip
, then {id}
will match foo.zip
and you tell Spring to store that value into the variable that has the annotation @PathVariable("id")
.
例如,如果用户访问/files/foo.zip
, then {id}
will matchfoo.zip
并且您告诉 Spring 将该值存储到具有注释的变量中@PathVariable("id")
。
You can have multiple path variable in a URL identifier of a @RequestMapping
annotation value, and you can inject these values into a variables by using @PathVariable
with the same id you used inside the curly brackets.
您可以在@RequestMapping
注释值的 URL 标识符中包含多个路径变量,并且您可以通过使用@PathVariable
与大括号内使用的相同 id将这些值注入到变量中。
回答by soniccol
I think for your example , by browsing ../files/1 or ../files/2 or ../files/3 , the digits stand for different file name . @PathVariable( "id" ) helps you save time write different parameter function on one purpose.
我认为对于您的示例,通过浏览 ../files/1 或 ../files/2 或 ../files/3 ,数字代表不同的文件名。@PathVariable("id") 帮助您节省时间,为一个目的编写不同的参数函数。
回答by Prawin Reddy
The {id} is the url query string we are passing what ever it may be and retrieving that id with @PathVariable("id") and passing as a argument to the method,one method fits for different requests with changing id here. Thanks.
{id} 是我们正在传递的 url 查询字符串,并使用 @PathVariable("id") 检索该 id 并将其作为参数传递给该方法,一种方法适合不同的请求,在这里更改 id。谢谢。
回答by anshu
@RequestMapping( value = "/files/{id}", method = RequestMethod.GET )
public void getFile( @PathVariable( "id" ) **String id**)
String fileName, HttpServletResponse response )
{
//your code here
}
pathvariable maps your uri with the method parameter. Here id is what you send with your request eg. /files/7.
pathvariable 使用方法参数映射您的 uri。这里 id 是您随请求发送的内容,例如。/文件/7。