【问题标题】:Spring JSP: Trouble with spring form with modelAttribute="" and path=""Spring JSP:使用 modelAttribute="" 和 path="" 的 spring 表单出现问题
【发布时间】:2015-11-17 02:29:44
【问题描述】:

当只有一个字符串要传递给控制器​​时,使用 modelAttribute="" 标记和 path="" 标记对我没有意义。然而,当一个表单有多个文本框时,为它们创建一个对象模型实际上是有意义的。这样modelAttribute标签代表对象,即“Employee”,而path标签代表字段,即“firstName”、“lastName”、“salary”。

当你只想传递一个字符串时你会怎么做?我不应该用 getKey() 和 setKey() 或任何疯狂的“key”字段创建一个“Key”类,只是为了将字符串传递给控制器​​方法,对吧?这种情况下的约定是什么?

如果我只是在页面加载时执行model.addAttribute("key", ""),我会得到:

org.springframework.beans.NotReadablePropertyException: Invalid property 'key' 
of bean class [java.lang.String]: Bean property 'key' is not readable or has an invalid 
getter method: Does the return type of the getter match the parameter type of the setter?

如果我删除 modelAttribute="key" 标签,我会得到:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for 
bean name 'command' available as request attribute

JSP

<form:form method="post" action="myAction" modelAttribute="key">
   <td>
       <form:input path="key" value="${myValue}"/>
       <input type="submit" value="Submit"/>
   </td>
</form:form> 

控制器

@RequestMapping(value = "/myAction", method = RequestMethod.POST)
public String handleMyActionRequest(@ModelAttribute("key") String key, Model model) {

    // do stuff with key string.

    return HOME_PAGE_REDIRECT;
}

在提交表单时将单个字符串传递给控制器​​方法的约定是什么,而不必创建新类?

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案1】:

    我刚刚有了这个想法,但我真的不知道它是否可以普遍建议。毕竟,这只是您想要避免的流程的内嵌版本。无论如何,我走了:

    在你的支持 bean 中,像这样将字符串添加到模型中:

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView home(Locale locale, Model model) {
    
        Object theTempBean = new Object(){
            public String key = "blahblahblah";
    
            public String getKey() {
                return key;
            }
    
            public void setKey(String key) {
                this.key = key;
            }
        };
    
        model.addAttribute("theTempBean", theTempBean);
    
        String viewName = "home";
        return new ModelAndView(viewName, "command", model);
    }
    

    JSP 应该如下所示:

        <form:form action="/myAction" modelAttribute="theTempBean">
    
            <form:input path="key" /> 
                <input
                    type="submit" value="Submit" />
        </form:form>
    

    那么,处理表单发布的 web 控制器的方法应该如下所示:

    @RequestMapping(path = "/myAction")
    public String myAction(@RequestParam("key") String param){
        logger.info(param);
        return "home";
    }
    

    我测试了这个简单的示例,它在 Spring 版本 4.2.0.RELEASE 和 Jetty Maven 插件版本 9.3.2.v20150730 上按预期工作。

    编辑

    有一个错误。如果您决定这样做,您必须在任何请求中设置“theTempBean”(也许它可以变成@ModelAttribute。同样,它只是一个额外 bean 类的内联版本)。这是固定动作处理程序方法:

    @RequestMapping(path = "/myAction")
    public String myAction(@RequestParam("key") String param
            , Model model){
        logger.info(param);
    
        Object theTempBean = new Object(){
            public String key = param;
    
            public String getKey() {
                return key;
            }
    
            public void setKey(String key) {
                this.key = key;
            }
        };
    
        model.addAttribute("theTempBean", theTempBean);
    
        return jspViewName("home");
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2012-08-12
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多