繁体   English   中英

Spring JSP:使用modelAttribute =“”和path =“”的spring表单出现问题

[英]Spring JSP: Trouble with spring form with modelAttribute=“” and path=“”

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

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

如果我只是在页面加载时执行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;
}

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

我刚才有了这个想法,但我真的不知道它是否一般都是可取的。 毕竟,它只是您想要避免的流程的在线版本。 无论如何,我去了:

在您的支持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");
}

暂无
暂无

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

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