简体   繁体   中英

Display JSR-303 errors before form submission with Spring MVC

Is there an easy way in Spring MVC 3.x to display form error messages (obtained by JSR303 validation), before submiting the form ?

Consider the example code at the end of this post.

The end-user is supposed to edit forms in which the initial data is already invalid.

Errors are properly displayed when the form is submitted (POST method), but not on initial form display (GET method).

Is there an easy way to display the errors on initial form display (GET method) ? (Is there a way to re-use the form:errors tag for this purpose?)

JSP View 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 :

@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";
    }
}

}

Backing Bean :

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
}
//...
}

Edit: After the interpreting the answer from @jb-nizet, here is the complete controller source :

@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";
    }
}

}

Made a few tests, and it seems to work! Than you @jb-nizet

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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