在Spring 3.0 GET请求中,@ PathVariable和@RequestParam之间的区别是什么 [英] In a Spring 3.0 GET request, what's the difference between a @PathVariable and a @RequestParam
问题描述
在如下例子中, @PathVariable
与 @RequestParam
之间有什么区别?
In an example such as the following, what's the difference between a @PathVariable
and a @RequestParam
?
@RequestMapping(value = "/portfolio/{portfolioIdPath}", method = RequestMethod.GET)
public final String portfolio(HttpServletRequest request, ModelMap model,
@PathVariable long portfolioIdPath, @RequestParam long portfolioIdRequest)
推荐答案
@RequestParam 将请求参数绑定到方法中的参数。在您的示例中,GET请求中名为portfolioIdRequest的参数的值将作为portfolioIdRequest参数传递给您的方法。一个更具体的例子 - 如果请求URL是
@RequestParam binds a request parameter to a parameter in your method. In your example, the value of the parameter named "portfolioIdRequest" in the GET request will be passed as the "portfolioIdRequest" argument to your method. A more concrete example - if the request URL is
http://hostname/portfolio/123?portfolioIdRequest=456
然后参数portfolioIdRequest的值将是456。
then the value of the parameter "portfolioIdRequest" will be "456".
@PathVariable 类似地将URI模板变量portfolioIdPath的值绑定到方法参数portfolioIdPath。例如,如果您的URI是
@PathVariable similarly binds the value of the URI template variable "portfolioIdPath" to the method parameter "portfolioIdPath". For example, if your URI is
/portfolio/123
然后portfolioIdPath方法参数的值将为123。
then the value of "portfolioIdPath" method parameter will be "123".
这篇关于在Spring 3.0 GET请求中,@ PathVariable和@RequestParam之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!