java - @RequestParam注解

标签 java spring annotations

我的问题可能看起来很奇怪,因为我是 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/

相关文章:

java - Apache CXF java2wsdl : Custom symbol names

java - Hibernate 发出个人查询而不是加入

Java Annotations - 寻找 RetentionPolicy.CLASS 的示例

java - 摆脱 "The value for annotation attribute must be a constant expression"消息

java - 图形对象不会随着 xPosition 的更新而更新

java - 我在一个 fragment 中有 2 个 RecyclerViews。但只显示第一个

json - 使用HTTPBuilder和Grails进行REST调用后出现奇怪的 “new”属性

java - 在Spring Boot应用程序中,不执行基于注释的切入点

java - BadSqlGrammarException : bad SQL grammar [SELECT JOB_INSTANCE_ID, BATCH_JOB_INSTANCE 中的 JOB_NAME...] - 不同环境下的不同行为

java - 使用 Java Annotation Processor 添加注释