繁体   English   中英

在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什么区别?

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

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

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

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

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

/portfolio/123

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

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

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

http://host/?var=1

在上面的URL中, “var”是一个requestparam。

http://host/registration/{which}

以上URL的{which}是请求映射。 您可以将您的服务称为:

http://host/registration/user

或者喜欢

http://host/registration/firm

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

这取决于您希望如何处理您的请求

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM