java 在 Spring 3.0 GET 请求中,@PathVariable 和 @RequestParam 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4073970/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:40:02  来源:igfitidea点击:

In a Spring 3.0 GET request, what's the difference between a @PathVariable and a @RequestParam

javaspringspring-mvc

提问by August

In an example such as the following, what's the difference between a @PathVariableand a @RequestParam?

在如下示例中, a@PathVariable和 a之间有什么区别@RequestParam

@RequestMapping(value = "/portfolio/{portfolioIdPath}", method = RequestMethod.GET)
public final String portfolio(HttpServletRequest request, ModelMap model, 
@PathVariable long portfolioIdPath, @RequestParam long portfolioIdRequest)

回答by benw

@RequestParambinds 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

@RequestParam将请求参数绑定到方法中的参数。在您的示例中,GET 请求中名为“portfolioIdRequest”的参数的值将作为“portfolioIdRequest”参数传递给您的方法。一个更具体的例子 - 如果请求 URL 是

http://hostname/portfolio/123?portfolioIdRequest=456

then the value of the parameter "portfolioIdRequest" will be "456".

那么参数“portfolioIdRequest”的值将是“456”。

More info here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

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

@PathVariablesimilarly binds the value of the URI template variable "portfolioIdPath" to the method parameter "portfolioIdPath". For example, if your URI is

@PathVariable类似地将 URI 模板变量“portfolioIdPath”的值绑定到方法参数“portfolioIdPath”。例如,如果您的 URI 是

/portfolio/123

then the value of "portfolioIdPath" method parameter will be "123".

那么“portfolioIdPath”方法参数的值将是“123”。

More info here: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

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

回答by Erhan Bagdemir

@RequestParam identifies the HTTP GET or POST parameter which is sent by the client(user), And @RequestMapping extracts a segment of URL which varies from request to request:

@RequestParam 标识客户端(用户)发送的 HTTP GET 或 POST 参数,@RequestMapping 提取一段 URL,该段因请求而异:

http://host/?var=1

In the above URL "var"is a requestparam.

在上面的 URL 中,“var”是一个请求参数。

http://host/registration/{which}

and above URL's {which}is a request mapping. You could call your service like :

和上面的 URL 的{which}是一个请求映射。你可以像这样调用你的服务:

http://host/registration/user

OR like

或喜欢

http://host/registration/firm

In your application you can access the value of {which}(In first case which="user" and in second which="firm".

在您的应用程序中,您可以访问{which}的值(第一种情况 which="user" 和第二种情况 which="firm"。

回答by sina

It depends the way you want to handle your request

这取决于您想要处理请求的方式

@RequestParam example
(request)http://something.com/owner?ownerId=1234

@PathVariable example
(request) http://something.com/owner/1234
(in tour code) /owner/{ownerId}