使用Spring MVC提交表单之前显示JSR-303错误 [英] Display JSR-303 errors before form submission with Spring MVC

查看:110
本文介绍了使用Spring MVC提交表单之前显示JSR-303错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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视图 form1.jsp:

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 @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来源:

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

}

进行了一些测试,它似乎可以工作!比你@ jb-nizet

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

推荐答案

未经测试,但据我了解在GET METHOD中添加错误的过程中进行了解释,并且调用验证程序的validate()方法,并将表单和错误作为参数传递.

Not tested, but as I understand the documentation, you would just have to inject an object of type org.springframework.validation.Validator in your controller, create and bind an Errors instance as explained in Add Error on GET METHOD Spring annotated controller, and invoke the validator's validate() method, passing the form and the Errors as argument.

这篇关于使用Spring MVC提交表单之前显示JSR-303错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆