[英]Spring form validation using annotation and @Valid
I am having some issues with my 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 and setters omitted
}
When i enter empty values (username, password and repeatedPassword) then no errors occurs (i have checked it using debugger). 当我输入空值(用户名,密码和repeatedPassword)时,不会发生任何错误(我已经使用调试器对其进行了检查)。 So it looks like no validation is performed. 因此,似乎没有执行验证。 Binding values from view is ok (checked using debugger) Any ideas what might be wrong? 从视图绑定值是可以的(使用调试器检查)。有什么想法可能是错误的吗?
Add following content into your context: 在您的上下文中添加以下内容:
<mvc:annotation-driven />
<context:component-scan base-package="xxx.xxx.xxx" />
In the guide, they use "@SpringBootApplication"
http://spring.io/guides/gs/validating-form-input/ 在指南中,他们使用"@SpringBootApplication"
http://spring.io/guides/gs/validating-form-input/
The @SpringBootApplication
annotation is equivalent to using @Configuration
, @EnableAutoConfiguration
and @ComponentScan
with their default attributes: http://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html @SpringBootApplication
注释等效于使用@Configuration
@EnableAutoConfiguration
, @ComponentScan
@EnableAutoConfiguration
和@ComponentScan
及其默认属性: http : @EnableAutoConfiguration
-annotation.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.