BindResult 삽질기!!!
회사 동료가 Spring Validation 기능을 활용해보자고 해서 해보는데 엄청난 시간 소모가...
결론은 BindResult 파라미터 선언을 @Valid 바로 뒤에 해야 한다는...
[TestWeb.java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import javax.validation.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping ( "test" ) public class TestWeb { private static Logger log = LoggerFactory.getLogger(TestWeb. class ); @RequestMapping (value= "case1" , method=RequestMethod.GET) public String test( @Valid TestVo testVo, BindingResult bindResult, Model model) { log.debug( "bindResult.hasErrors(): {}" , bindResult.hasErrors()); if (bindResult.hasErrors()) { throw new RuntimeException( "파라미터 설정이 잘 못 되었습니다.." ); //return "coupon/mainError"; // 공통 Exception 처리 Throws 하거나 에러 뷰 return } return "test/case1" ; } } |
[TestVo.java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import lombok.Getter; import lombok.Setter; @Getter @Setter public class TestVo { @Size (min= 0 , max= 5 ) private String test1; @NotNull private String test2; } |
[build.gradle]
1 2 3 4 | dependencies { compile 'spring.... 4.1.x...' compile 'org.hibernate:hibernate-validator:5.1.3.Final' } |