簡體   English   中英

使用Spring MVC提交表單之前顯示JSR-303錯誤

[英]Display JSR-303 errors before form submission with Spring MVC

在提交表單之前,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

沒有經過測試,但是據我了解的文檔 ,您只需要在控制器中注入org.springframework.validation.Validator類型的對象,創建並綁定Errors實例即可,如GET METHOD中的“ 添加錯誤”中所述。調用驗證器的validate()方法,並將表單和Errors作為參數傳遞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM