[英]When is @RequestParam annotation not needed in Spring MVC?
I'm looking through some Spring 3 MVC controller code and I see that @RequestParam is used for some parameters and not for others. 我正在查看一些Spring 3 MVC控制器代码,我发现@RequestParam用于某些参数而不是其他参数。 Example where it is not being used: 不使用它的示例:
@RequestMapping(value = "/experiments", method = RequestMethod.GET)
public String getExperimentsPage(ExperimentSearchCriteria criteria, Map<String, Object> model) {
// method body here
}
When is @RequestParam (or similar parameter-specifying annotation) not needed? 什么时候不需要@RequestParam(或类似的参数指定注释)?
Good question, I have been wondering this too until I found it being mentioned in the doc: 好问题,我一直在想这个,直到我发现它在文档中被提到:
Note that use of @RequestParam is optional, eg to set its attributes. 请注意,使用@RequestParam是可选的,例如设置其属性。 By default any argument that is a simple value type, as determined by BeanUtils#isSimpleProperty, and is not resolved by any other argument resolver, is treated as if it was annotated with @RequestParam. 默认情况下,由BeanUtils#isSimpleProperty确定的任何简单值类型的参数都不会被任何其他参数解析器解析,就像使用@RequestParam注释一样。
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam
Most of time, I don't specify this unless my method parameter name is different with request parameter, or, if the value is optional, I will need to use required=false
. 大多数时候,我没有指定这个,除非我的方法参数名称与request参数不同,或者,如果值是可选的,我将需要使用required=false
。
@RequestParam is used to pass query parameters. @RequestParam用于传递查询参数。
Example: http://localhost:8080/employee/get?status=ACTIVE 示例: http:// localhost:8080 / employee / get?status = ACTIVE
Here we can get status with below code 在这里,我们可以获得以下代码的状态
@RequestParam(value="status") String status @RequestParam(value =“status”)字符串状态
We have some properties like required, defaultValue etc. If you provide required=false as mentioned in below line of code, the status parameter is not mandatory in URL 我们有一些属性,如required,defaultValue等。如果你提供如下面的代码行中提到的required = false,则status中的status参数不是必需的
@RequestParam(value="status",required=false) @RequestParam(值= “状态”,需要=假)
The URL will be like http://localhost:8080/employee/get URL将类似于http:// localhost:8080 / employee / get
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.