【问题标题】:@ModelAttribute not works when a declared field is left blank当声明的字段留空时,@ModelAttribute 不起作用
【发布时间】:2013-03-08 07:16:55
【问题描述】:

下面是我的代码...

@RequestMapping(value = "/submitnewstory", method = RequestMethod.POST)
    @ResponseBody
    public String save(@ModelAttribute("storyInsertForm") StoryInsertForm uploadForm, HttpSession session, Model map) {
}

HTML 代码:

<form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">

</form:form>

发生了什么?

假设我的模型中有两个字段(在本例中为 StoryInsertForm),即 String firstnameString lastname。我必须在&lt;form:form&gt; 中声明相同的变量,如下所示

<form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">
    <input type="text" name="firstname" id="firstname" />
    <input type="text" name="lastname" id="lastname" />
</form:form>

发生的情况是,如果您给第一个输入一个值而忽略第二个输入,则数据甚至不会到达控制器。但是,如果您提供所有值,即为两种输入类型文本提供输入,它会找到控制器并且一切正常。

另一个观察表明,如果您不声明其中一个字段,即类似于以下内容:

<form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">
    <input type="text" name="firstname" id="firstname" />
<!--    <input type="text" name="lastname" id="lastname" /> -->
</form:form>

数据仍会与上述字段中提供的数据一起发布(在本例中为名字)。

总结

如果您已关联 html 中的字段,则必须在输入类型中输入一些值,否则控制器将无法接收任何内容。

期待您的帮助。

【问题讨论】:

    标签: html spring-mvc controller spring-roo


    【解决方案1】:

    我的想法,

    您还必须绑定表单输入标签才能使用

    Spring 表单标签库。

    <form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">
        <form:input  path="firstname" id="firstname" />
        <form:input  path="lastname" id="lastname" />
    </form:form>
    

    另外我不知道你的控制器编码是什么,所以我认为以下几点值得一提-

    1> 在页面作为RequestMethod GET返回的函数中,为表单添加一个模型属性。

    在这种情况下,完整编码应该是-

    @RequestMapping(value = "/getpage.htm", method = RequestMethod.POST)
    public String getPage(Model model){
            ........
            Other Code Snippet
            .........
    
        StoryInsertForm uploadForm = new StoryInsertForm();
        model.addAttribute(“storyInsertForm”,  uploadForm);
        return “login”;
    }
    

    2>还要在数据保存方法中添加BindingResult参数,否则你应该得到一个异常-

    所以保存方法的代码应该是-

    @RequestMapping(value = "/submitnewstory", method = RequestMethod.POST)
        @ResponseBody
        public String save(@ModelAttribute("storyInsertForm") StoryInsertForm uploadForm, BindingResult result,  HttpSession session, Model map) {
         ...........
         Your Save Business Logic and Other DAO Method call etc...
         ...........
    }
    

    希望以上代码能解决你的问题。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题;我无法解决它,但可以通过使用

      来规避它
      <form></form> 
      

      而不是

       <form:form></form:form> 
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2019-05-24
        • 2014-08-24
        • 2020-05-05
        • 2015-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多