java - Spring form validation using annotation and @Valid -
i having issues form validation.
controller:
@requestmapping(value = register_url, method = requestmethod.post) public string registerpost(@valid registerform registerform, bindingresult result) { if (result.haserrors()) { return register_view; } system.out.println(registerform.getpassword()); return login_view; }
view:
<form:form action="register" commandname="registerform" method="post"> <table> <tr> <td>username:</td> <td><form:input path='username' /></td> <td><form:errors path="username"/></td> </tr> <tr> <td>password:</td> <td><form:password path='password'/></td> <td><form:errors path="password"/></td> </tr> <tr> <td>repeat password:</td> <td><form:password path='repeatedpassword'/></td> <td><form:errors path="repeatedpassword"/></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td colspan='2'><input name="submit" type="submit"> <input name="reset" type="reset"></td> </tr> </table> </form:form>
form:
public class registerform { @size(min = 3, max = 15) private string username; @size(min = 5) private string password; @size(min = 5) private string repeatedpassword; // getters , setters omitted }
when enter empty values (username, password , repeatedpassword) no errors occurs (i have checked using debugger). looks no validation performed. binding values view ok (checked using debugger) ideas might wrong?
add following content context:
<mvc:annotation-driven /> <context:component-scan base-package="xxx.xxx.xxx" />
in guide, use "@springbootapplication"
http://spring.io/guides/gs/validating-form-input/
the @springbootapplication
annotation equivalent using @configuration
, @enableautoconfiguration
, @componentscan
default attributes: http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html
Comments
Post a Comment