简体   繁体   English

@RequestParam批注

[英]@RequestParam annotation

My question may seem to be curious, because I'm newbie in Java. 我的问题似乎很好奇,因为我是Java的新手。

I've read the oracle lesson about annotation in Java . 我已经阅读了有关Java注释的oracle 课程 But I have not understood yet how they work on practice. 但是我还不了解他们如何实践。 Consider the following spring framework 4.0.1 defined annotation: 考虑以下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";
}

The annotation may apply to a function parameter like the following 注释可能适用于如下功能参数

 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
    }

Who exactly assign requested parameter value to a corresponding function parameter? 谁精确地将请求的参数值分配给相应的功能参数?

The default ( @EnabledWebMvc or <mvc:annotation-driven /> ) MVC stack in the Spring version you are using uses implementations of HandlerMethodArgumentResolver to resolve arguments to use when invoking the handler method (method annotated with @RequestMapping ). 您使用的Spring版本中的默认MVC堆栈( @EnabledWebMvc<mvc:annotation-driven /> )使用HandlerMethodArgumentResolver实现来解析调用处理程序方法时使用的参数(以@RequestMapping注释的方法)。

For @RequestParam , that implementation is RequestParamMethodArgumentResolver . 对于@RequestParam ,该实现为RequestParamMethodArgumentResolver

The arguments are all collected, then Spring uses reflection to invoke your handler method. 所有的参数都被收集,然后Spring使用反射来调用您的处理程序方法。 It passes the collected arguments during the invocation. 它在调用期间传递收集的参数。

The default @EnabledWebMvc or < mvc:annotation-driven /> MVC stack in the Spring version you are using uses implementations of HandlerMethodArgumentResolver to resolve arguments to use when invoking the handler method (method annotated with @RequestMapping ). 您使用的Spring版本中的默认@EnabledWebMvc<mvc:annotation-driven /> MVC堆栈使用HandlerMethodArgumentResolver实现来解析调用处理程序方法时要使用的参数(用@RequestMapping注释的方法)。

For @RequestParam , that implementation is RequestParamMethodArgumentResolver . 对于@RequestParam ,该实现为RequestParamMethodArgumentResolver

The arguments are all collected, then Spring uses reflection to invoke your handler method. 所有的参数都被收集,然后Spring使用反射来调用您的处理程序方法。 It passes the collected arguments during the invocation. 它在调用期间传递收集的参数。

In Spring MVC you have to apply ; 在Spring MVC中,您必须申请;

1- index.jsp in WEB-INF
2- property names for index.jsp in view.xml
3- Controller for property names
4- mapping for controller in servlet.xml

If you want use @RequestParam in a project you need ; 如果要在项目中使用@RequestParam,则需要使用它;

-Form action in jsp
-Class for variables
-Controller for form variables.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM