Spring 未强制执行表单输入约束?
我是Tomcat和SpringWeb的新手。我试图通过以下方式使用Spring的表单验证特性。除了一件事之外,一切似乎都很顺利。。。我的表单不做任何验证,无论我提供了哪些数据,当我发送表单时,我总能进入成功页面 我是否正确使用了约束?我想强制用户填写他们的名字,并且名字至少有两个字符长Spring 未强制执行表单输入约束?,spring,tomcat,spring-mvc,Spring,Tomcat,Spring Mvc,我是Tomcat和SpringWeb的新手。我试图通过以下方式使用Spring的表单验证特性。除了一件事之外,一切似乎都很顺利。。。我的表单不做任何验证,无论我提供了哪些数据,当我发送表单时,我总能进入成功页面 我是否正确使用了约束?我想强制用户填写他们的名字,并且名字至少有两个字符长 package net.devmanuals.form; import javax.validation.constraints.Size; import org.hibernate.validator.con
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>
我是否错误地应用了约束?除了添加到配置之外,还需要确保JSR-303 jar位于类路径上。从:
[AnnotationDrivenBeandDefinitionParser]。。。配置验证器(如果指定),否则默认为由默认LocalValidatoryBean创建的新验证器实例(如果类路径上存在JSR-303API)
你的配置里有吗?哎呀。我尝试将其添加到Dispatcher-servlet.xml中,但在部署后出现以下错误:元素mvc:annotation driven的前缀mvc未绑定。请在配置中为mvc添加名称空间,即xmlns:mvc=…在将slf4j-api-1.6.1.jar添加到我的库列表中后,我已经有了Hibernate验证器,一切似乎都在顺利运行。
@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";
}
}