0

在提交表单之前,Spring MVC 3.x 中是否有一种简单的方法来显示表单错误消息(通过 JSR303 验证获得)?

考虑本文末尾的示例代码。

最终用户应该编辑初始数据已经无效的表单。

提交表单时会正确显示错误(POST 方法),但不会在初始表单显示时(GET 方法)。

是否有一种简单的方法可以在初始表单显示(GET 方法)上显示错误?(有没有办法为此目的重新使用 form:errors 标签?)

JSP查看form1.jsp:

<%@ page session="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html><body>
<form:form commandName="form1" method="post">
 <s:bind path="*">
   <c:if test="${status.error}">
       <div style="color: red;">There are errors:</div>
       <form:errors /><br />
   </c:if>
  </s:bind>

  <form:label path="field1" >Field1:</form:label> 
  <form:input path="field1" />
  <form:errors path="field1" cssStyle="color: red;"/>
  <br />

  <form:label path="field2" >Field2:</form:label> 
  <form:input path="field2" />
  <form:errors path="field2" cssStyle="color: red;"/>
  <br />

  <form:button name="submit">Ok</form:button>
</form:form>
</body></html>

控制器

@Controller @SessionAttributes("form1")
public class Form1Controller {

@ModelAttribute("form1") public Form1Bean createForm1() { return new Form1Bean(); }

@RequestMapping(value = "/form1/edit", method = RequestMethod.GET)
public String getEdit(Model model) {
    // here we pretend to get form1 from database, and store it in session.
    // form1 in database may have invalid field values.

    // Perform a JSR-303 validation here ?
    // MAIN QUESTION: What's the easy way to add errors to model and display them ?

    return "/form1";
}

@RequestMapping(value = "/form1/edit", method = RequestMethod.POST)
public String postEdit(@Valid @ModelAttribute("form1") Form1Bean form1, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "/form1";
    } else {
        return "redirect:/done";
    }
}

}

支持豆

public class Form1Bean {
@Size(min=4,max=10) private String field1; // getters and setters ommited for brevity
@Size(min=4,max=10) private String field2;

public Form1Bean() {
    this.field1 = "bad"; this.field2="good"; // start with an invalid field1
}
//...
}

编辑:从@jb-nizet 解释答案后,这里是完整的控制器源代码

@Controller @SessionAttributes("form1")
public class Form1Controller {

@Autowired org.springframework.validation.Validator validator;

@ModelAttribute("form1") public Form1Bean createForm1() { return new Form1Bean(); }

@RequestMapping(value = "/form1/edit", method = RequestMethod.GET)
public String getEdit(@ModelAttribute("form1") Form1Bean form1, Errors errors, Model model) {
    // here we pretend to get form1 from database, and store it in session.
    // form1 in database may have invalid field values.

    validator.validate(form1, errors);

    return "/form1";
}

@RequestMapping(value = "/form1/edit", method = RequestMethod.POST)
public String postEdit(@Valid @ModelAttribute("form1") Form1Bean form1, BindingResult result, Model model) {
    if (result.hasErrors()) {
        return "/form1";
    } else {
        return "redirect:/done";
    }
}

}

做了一些测试,它似乎工作!比你@jb-nizet

4

1 回答 1

0

未经测试,但据我了解文档,您只需org.springframework.validation.Validator在控制器中注入类型对象,创建并绑定错误实例,如在 GET METHOD Spring 注释控制器上添加错误中所述,然后调用验证器的 validate() 方法,将表单和错误作为参数传递。

于 2013-05-17T22:20:24.100 回答