简体   繁体   English

什么时候选择@RequestParam而不是@PathVariable,反之亦然?

[英]When to choose @RequestParam over @PathVariable and vice-versa?

I know what @RequestParam does and what @PathVariable does on an individual note but my question is when building a URL, when should I go for @RequestParam and when for @PathVariable ? 我知道@RequestParam作用以及@PathVariable在单个注释上的作用,但我的问题是构建URL时,何时应使用@RequestParam以及何时应使用@PathVariable What are the pros and and cons of using both these annotations? 同时使用这两个注释的利弊是什么?

I did go through this link When to go for @RequestParam and @PathVariable but did not get a satisfactory answer anywhere. 我确实通过了此链接, 何时需要@RequestParam和@PathVariable,但是在任何地方都没有得到满意的答案。

@PathVariable identifies the pattern that is used in the URI for the incoming request. @PathVariable标识在URI中用于传入请求的模式。

Query args ( @RequestParam ) are used on querying/searching resources exclusively. 查询参数( @RequestParam )仅用于查询/搜索资源。 They contain data that affects the query. 它们包含影响查询的数据。

Here are some tips for creating good URLs. 以下是创建良好网址的一些技巧

It makes more sense to use @Pathvariable to give the user an idea of what the page means. 使用@Pathvariable可以使用户对页面的含义有所了解。
twitter.com/{userid}
@RequestParams can be used to get info as parameter, like twitter.com?search=%23yolo @RequestParams可用于获取信息作为参数,例如twitter.com?search=%23yolo
@Pathvariable will help the user navigate or directly move to certain views/pages. @Pathvariable将帮助用户导航或直接移动到某些视图/页面。

@PathVariable: @PathVariable:

When u need to pass parameters along with the url to get the data. 当您需要将参数与url一起传递以获取数据时。 In Spring MVC, u can customize the URL in order to get data. 在Spring MVC中,您可以自定义URL以获取数据。 For this purpose @PathVariable annotation is used in Spring framework. 为此,在Spring框架中使用@PathVariable批注。 @PathVariable is used to obtain some placeholder from the uri (Spring call it an URI Template). @PathVariable用于从uri获取一些占位符(Spring将其称为URI模板)。

@RequestParam: @RequestParam:

Is used to obtain an request parameter. 用于获取请求参数。 @RequestParam binds a request parameter to a parameter in your method. @RequestParam将请求参数绑定到方法中的参数。

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

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