何时去@RequestParam和@PathVariable [英] When to go for @RequestParam and @PathVariable

查看:123
本文介绍了何时去@RequestParam和@PathVariable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道在哪种情况下我们应该选择 @RequestParam @PathVariable 。我知道:

Just curious to know in which scenario we should go for @RequestParam and @PathVariable. I know that:


  1. @RequestParam 获取参数值而 @PathVariable 占位符值

  2. @RequestParam 在提出请求时可以是可选的(必需= false)而必须提供 @PathVariable 值。

  3. 当我们想要使用 @RequestParam 时,我们必须知道属性语法,但 @PathVariable 不需要

  1. @RequestParam takes parameter value whereas @PathVariable takes placeholder value
  2. @RequestParam can be optional (required=false) while making request whereas @PathVariable value has to be provided.
  3. When we want to use @RequestParam we have to know the property syntax but for @PathVariable not required

还有其他理由去特定的吗?

Is there any other reason to go for specific one?

推荐答案

如果你想遵守'statefull'网址,请使用 @PathVariable

Use @PathVariable if you want to adhere to 'statefull' URLs.

例如: -

/customer/:id   Customer view/edit page
/customer/      Customer Add page
/customer/list  List Customer Page
/customer/:cid/order  All order of a Customer
/customer/:cid/order/:oid  Specific order of a partucular Customer.

明智地使用路径变量将导致URL提供关于结果视图/页面的提示/线索手段。

Wisely using Path Variable will result in URL that gives you hint/clue about what the resulting view/page means.


这也可以让你支持刷新,返回&没有
额外工作的正向操作。

This also lets you support refresh,back & forward operation with no extra effort.

@RequestParams可用于解除未作为路径参数传递的数据。您的MVC处理程序可以根据需要组合两个。

@RequestParams can be used to exatract data which is not passed as path params. Your MVC handler can have combination of two as required.

这篇关于何时去@RequestParam和@PathVariable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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