【问题标题】:@RequestParam annotation@RequestParam 注解
【发布时间】:2014-07-22 14:38:21
【问题描述】:

我的问题似乎很好奇,因为我是 Java 新手。

我已经阅读了关于 Java 中注释的 oracle lesson。但我还不明白他们在实践中是如何工作的。考虑以下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
    }

谁将请求的参数值准确地分配给相应的函数参数?

【问题讨论】:

    标签: java spring annotations


    【解决方案1】:

    您正在使用的 Spring 版本中的默认(@EnabledWebMvc<mvc:annotation-driven />)MVC 堆栈使用 HandlerMethodArgumentResolver 的实现来解析调用处理程序方法时要使用的参数(使用 @RequestMapping 注释的方法)。

    对于@RequestParam,该实现是RequestParamMethodArgumentResolver

    所有参数都被收集起来,然后 Spring 使用反射来调用您的处理程序方法。它在调用期间传递收集的参数。

    【讨论】:

      【解决方案2】:

      您使用的 Spring 版本中的默认 @EnabledWebMvc MVC 堆栈使用 HandlerMethodArgumentResolver 的实现来解析要使用的参数调用处理程序方法时(使用@RequestMapping 注释的方法)。

      对于@RequestParam,该实现是RequestParamMethodArgumentResolver

      所有参数都被收集起来,然后 Spring 使用反射来调用您的处理程序方法。它在调用期间传递收集的参数。

      【讨论】:

        【解决方案3】:

        在 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
        

        如果你想在你需要的项目中使用@RequestParam ;

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

        【讨论】:

        • 不清楚。我想,在jsp中没有必要有坚定的行动。控制器的动作可能只针对业务逻辑。
        • 是的。这只是一个例子。你可以设置你的类变量,我认为你可以使用@requestParam
        猜你喜欢
        • 1970-01-01
        • 2014-12-29
        • 2020-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多