2013-05-17 1 views
0

양식을 제출하기 전에 Spring MVC 3.x에서 JSR303 유효성 검사에서 얻은 양식 오류 메시지를 표시하는 쉬운 방법이 있습니까?Spring MVC로 양식 제출 전에 JSR-303 오류 표시

이 게시물의 끝 부분에있는 예제 코드를 살펴보십시오.

최종 사용자는 초기 데이터가 이미 유효하지 않은 양식을 편집해야합니다.

양식이 제출 될 때 (POST 메소드) 오류는 올바르게 표시되지만 초기 양식 표시 (GET 메소드)에서는 표시되지 않습니다.

초기 양식 표시 (GET 방법)에서 오류를 쉽게 표시 할 수 있습니까? (양식 다시 사용하는 방법이 있나요?이 목적을 위해 오류 태그)

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 
} 
//... 
} 
012 3,516,

편집 :이 @의 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"; 
    } 
} 

} 

는 몇 가지 검사를 제작하고, 그것을 작동하는 것 같다! 내가 the documentation을 이해하면 @ JB-nizet

답변

0

테스트는 아니지만, 그러나, 당신은, 당신의 컨트롤러 타입 org.springframework.validation.Validator의 객체를 주입 만들고 Add Error on GET METHOD Spring annotated controller에 설명 된대로 오류 인스턴스를 바인딩과 유효성 검사기의 유효성 검사를 호출해야() 메서드를 호출하여 폼과 오류를 인수로 전달합니다.

+0

위대한 답변! 고맙습니다. – user1544553