在Spring 3.0 GET请求中,@ PathVariable和@RequestParam之间的区别是什么 [英] In a Spring 3.0 GET request, what's the difference between a @PathVariable and a @RequestParam

查看:136
本文介绍了在Spring 3.0 GET请求中,@ PathVariable和@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".

此处有更多信息 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

@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".

此处更多信息 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

这篇关于在Spring 3.0 GET请求中,@ PathVariable和@RequestParam之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆