验证失败后,将清除默认的弹簧形式字段值

时间:2012-06-25 19:07:09

标签: spring-mvc

我是springMVC的新手,遇到了这个我不明白的问题。我有一个带有表单的JSP,其中一些字段在我的get方法中预先填充了一些 我从数据库中获得的值。当我通过POST方法提交表单并且未填写必填字段时,我调用我的GET方法再次显示表单 页面顶部的错误消息。一切都很好,我需要预先填充的字段可以正确填充。

我们在测试中遇到的问题是,当我们删除预先填充的字段并再次提交时,我的POST方法调用我的GET方法重新显示页面时出错 消息和错误消息在屏幕上显示正常但我的字段假设预先填充了一些值,甚至没有再次重新填充 虽然我每次调用数据库并使用我的GET方法中的model.put()方法填充我的表单。

如果有错误,似乎spring不会填充JSP上的任何字段。是吗?

这是我控制器的一个简化版本,但它会解释我正在尝试做的事情:

@RequestMapping(value = myPageURL, method = GET)
public String displayPage(@ModelAttribute("someForm") SomeForm someForm,
                          BindingResult bindingResult,
                          ModelMap model) {

    //call the database and get some values
    someform.setSomefield1("someValue1");
    someform.setSomefield2("someValue2");
    model.put("someform", someForm)

        return VIEW;
}

@RequestMapping(value = myPageURL, method = POST)
public String submitform(@ModelAttribute("someForm") SomeForm someForm,
                                     BindingResult bindingResult,
                                     ModelMap model) {
validate(someForm, bindingResult);
    if (!bindingResult.hasErrors()) {

        //Display some page
    }

    return displayPage(someForm, bindingResult, model);

}

这是JSP的一部分:

<form:form action=myPageURL method="POST" modelAttribute="someForm">
<form:hidden path="cid"/>
  <td>
    <form:input path="someField1" size="10" maxlength="20" cssErrorClass="fieldError      
       error-text-first-name-label"/>
  </td>
  <td>
    <form:input path="someField2" size="10" maxlength="20" cssErrorClass="fieldError 
     error-text-last-name-label"/>
 </td>
 <td>
   <form:input path="someField3" size="10" maxlength="20" cssErrorClass="fieldError 
    error-text-last-name-label"/>
 </td>

</form:form>

由于

0 个答案:

没有答案