表单输入约束未强制执行?
我是 Tomcat 和 Spring Web 的新手。我正在尝试按照 this 使用 Spring 的表单验证功能教程。一切似乎都运行顺利,除了一件事......我的表单不进行任何验证,当我发送表单时,无论我提供哪些数据,我总是可以到达成功页面。
我是否正确使用了约束?我想强制用户填写他们的名字,并且名字至少有两个字符长。
package net.devmanuals.form;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class RegistrationForm {
@NotEmpty(message = "You surely have a name, don't you?")
@Size(min = 2, message = "I'm pretty sure that your name consists of more than one letter.")
private String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
}
表单代码:
<form:form method="post" commandName="regform">
<p><form:input path="firstName" /> <form:errors path="firstName" /></p>
<p><input type="submit" /></p>
</form:form>
控制器:
@Controller
@RequestMapping("/register")
public class RegistrationController {
@RequestMapping(method = RequestMethod.GET)
public String showRegForm(Map model) {
RegistrationForm regForm = new RegistrationForm();
model.put("regform", regForm);
return "regform";
}
@RequestMapping(method = RequestMethod.POST)
public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Map model) {
if (result.hasErrors()) {
return "regform";
}
model.put("regform", regForm);
return "regsuccess";
}
}
我是否错误地应用了约束?
I'm new to Tomcat and Spring Web. I'm trying to use Spring's form validation features by following this tutorial. Everything seems to run smoothly except for one thing... my form doesn't do any validation and I can always get to the success page when I send the form no matter which data I provide.
Am I using the constraints correctly? I want to enforce that the user fills in their first name and that the first name be at least two characters long.
package net.devmanuals.form;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class RegistrationForm {
@NotEmpty(message = "You surely have a name, don't you?")
@Size(min = 2, message = "I'm pretty sure that your name consists of more than one letter.")
private String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
}
Form code:
<form:form method="post" commandName="regform">
<p><form:input path="firstName" /> <form:errors path="firstName" /></p>
<p><input type="submit" /></p>
</form:form>
The controller:
@Controller
@RequestMapping("/register")
public class RegistrationController {
@RequestMapping(method = RequestMethod.GET)
public String showRegForm(Map model) {
RegistrationForm regForm = new RegistrationForm();
model.put("regform", regForm);
return "regform";
}
@RequestMapping(method = RequestMethod.POST)
public String validateForm(@Valid RegistrationForm regForm, BindingResult result, Map model) {
if (result.hasErrors()) {
return "regform";
}
model.put("regform", regForm);
return "regsuccess";
}
}
Am I applying the constraints incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了将
添加到您的配置中之外,您还需要确保 JSR-303 jar 位于您的类路径中。来自 文档:In addition to adding
<mvc:annotation-driven/>
to your config, you need to make sure the JSR-303 jar is on your classpath. From the docs: