Tomcat 및 Spring Web을 처음 사용했습니다. 나는 this tutorial에 따라 Spring의 폼 유효성 검사 기능을 사용하려고합니다. 모든 것이 한 가지를 제외하면 원활하게 진행되는 것 같습니다 ... 내 양식은 유효성 검사를하지 않으며 내가 제공 한 데이터에 관계없이 양식을 보내면 언제나 성공 페이지로 이동할 수 있습니다.양식 입력 제한 사항이 적용되지 않습니까?
제약 조건을 올바르게 사용하고 있습니까? 사용자가 이름을 채우고 이름의 길이를 2 자 이상으로 유지해야합니다.
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";
}
}
내가 잘못 제약 조건을 적용하는 건가요?
설정에' '이 있습니까? –
skaffman
내 Dispatcher-servlet.xml 파일에 추가하려고 시도했지만 배포 후 다음 오류가 발생합니다. * 요소 "mvc : annotation-driven"에 대한 접두어 "mvc"가 바인딩되지 않았습니다. * – Pieter
구성에 mvc의 네임 스페이스 추가 , 즉 xmlns : mvc = "http://www.springframework.org/schema/mvc"..... –