我在基于注释表单的工作中获得弹簧验证时遇到了一些麻烦。
我将此添加到我的 spring-servlet.xml
<context:component-scan base-package="com.it.controller" />
(包含我所有控制器的包)
和
<context:component-scan base-package="com.it.form" />
(包含我所有表单类的包)
com.it.form 包中的类电子邮件:
public class email {
@NotEmpty
@Email
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public email() {
email = "";
// TODO Auto-generated constructor stub
}
}
形式 :
<body>
<form:form method="post" action="" commandName='email'>
<div class="requestEmail">
<form:label path="email">Entrez votre email:</form:label>
<form:input path="email" />
<form:errors path="email" />
</div>
<div>
<input type="submit" value="VALIDER" />
</div>
</form:form>
控制器 :
@Controller
@SessionAttributes
public class passwordController {
/*
* ##########################################################
*
* Print & valid form Email
*
* ##########################################################
*/
@RequestMapping(value = "/passwordChange.mvc", method = RequestMethod.GET)
public String get(final ModelMap model) {
email email= new email();
model.addAttribute("email", email);
return "passwordChangeRequestEmail"; // jsp form
}
@RequestMapping(value = "/passwordChange.mvc", method = RequestMethod.POST)
public String post(final ModelMap model,
@ModelAttribute("email") @Valid final email email,
final BindingResult result) {
if (result.hasErrors()) {
return "error";
}
return "success";
}
}
似乎当我提交表单时,我总是重定向到 /success 页面,即使我将电子邮件输入留空...
不知道如果我错过了什么提前谢谢:)