我的问题可能看起来很奇怪,因为我是 Java 新手。
我读过神谕lesson关于Java
中的注释。但我还不明白他们在实践中是如何运作的。考虑以下 spring Framework 4.0.1
定义的注释:
@Target(value = {ElementType.PARAMETER})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
public String value() default "";
public boolean required() default true;
public String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
}
注释可以应用于如下所示的函数参数
public void change(@RequestParam("id") String id, @RequestParam("cost") String cost, @RequestParam("name") String name, Model model) throws SQLException, UnsupportedEncodingException {
//id will have the value of param id, passing inside request
//but ***I have no explicitly assignation*** requested parameter
//to the function parameter
}
到底是谁将请求的参数值分配给相应的函数参数?
最佳答案
您正在使用的 Spring 版本中的默认( @EnabledWebMvc
或 <mvc:annotation-driven />
)MVC 堆栈使用 HandlerMethodArgumentResolver
的实现解析调用处理程序方法时要使用的参数(用 @RequestMapping
注释的方法)。
对于@RequestParam
,该实现是 RequestParamMethodArgumentResolver
.
参数全部收集完毕,然后 Spring 使用反射来调用您的处理程序方法。它在调用期间传递收集的参数。
关于java - @RequestParam注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890351/