gpt4 book ai didi

java - Spring MVC 什么时候不需要 @RequestParam 注解?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:18:07 27 4
gpt4 key购买 nike

我正在查看一些 Spring 3 MVC Controller 代码,我看到 @RequestParam 用于某些参数而不用于其他参数。未使用的示例:

@RequestMapping(value = "/experiments", method = RequestMethod.GET)
public String getExperimentsPage(ExperimentSearchCriteria criteria, Map<String, Object> model) {
// method body here
}

什么时候不需要@RequestParam(或类似的参数指定注解)?

最佳答案

好问题,我也一直想知道这个问题,直到我发现它在文档中被提及:

Note that use of @RequestParam is optional, e.g. to set its attributes. 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.

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam

大多数时候,我不会指定它,除非我的方法参数名称与请求参数不同,或者,如果该值是可选的,我将需要使用 required=false .

关于java - Spring MVC 什么时候不需要 @RequestParam 注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16205990/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com