简体   繁体   中英

@RequestParam annotation

My question may seem to be curious, because I'm newbie in Java.

I've read the oracle lesson about annotation in Java . But I have not understood yet how they work on practice. Consider the following spring framework 4.0.1 defined annotation:

@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 ).

For @RequestParam , that implementation is RequestParamMethodArgumentResolver .

The arguments are all collected, then Spring uses reflection to invoke your handler method. 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 ).

For @RequestParam , that implementation is RequestParamMethodArgumentResolver .

The arguments are all collected, then Spring uses reflection to invoke your handler method. It passes the collected arguments during the invocation.

In Spring MVC you have to apply ;

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 ;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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