我不断看到这种参数value = "/redirect/{id}"
in a @RequestMapping
Spring 的注释。我一直想知道什么是{id}
这里?这是某种Expression Language
?
我所看到的示例代码:
@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" );
}
}
我的问题是什么{id}
在映射中以及它与@PathVariable
注释以及如何使用它?我从网上获取了一些信息,但如果能听到你们更清晰的解释,我会更加感激。
The {foo}
参与一个@RequestMapping
value 是一个路径变量,表示从 url 路径而不是从请求参数检索的值。
例如,如果用户访问/files/foo.zip
, then {id}
将匹配foo.zip
然后告诉 Spring 将该值存储到具有注释的变量中@PathVariable("id")
.
您可以在 URL 标识符中包含多个路径变量@RequestMapping
注释值,您可以使用以下方法将这些值注入到变量中@PathVariable
与您在大括号内使用的 id 相同。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)