Spring 3验证方法未验证
我在寻找如何在 Spring 3 中进行验证时遇到了一些问题。 到目前为止我所拥有的是这个 Java Bean....
public class User {
@NotEmpty
@Size(min=2, max=40)
private String name="";
@Email
private String email="";
public String getName(){
return name;
}
public String getEmail(){
return email;
}
public void setName(String name){
this.name=name;
}
public void setEmail(String email){
this.email=email;
}
}
这是链接到包含表单的页面的方法:
@RequestMapping(value="/form", method=RequestMethod.GET)
public String form(Model model){
model.addAttribute("user",new User());
return "form";
}
这是我的控制器中的方法,它“应该”验证
@RequestMapping(value="/displayUser",method=RequestMethod.POST)
public String displayUser(Model model,@Valid User user,BindingResult result){
if(result.hasErrors()){
return "form";
}
userList.add(user);
model.addAttribute("user",user);
return "displayUser";
}
@Validate 注释应该进行验证,但就我而言,事实并非如此。
你看到我的代码有什么问题吗?
顺便说一句,这是我的形式“一切都非常简单,这只是为了测试目的
<f:form method="post" action="displayUser" commandName="user">
<table align="center" border="1">
<tr>
<td>Your details</td>
</tr>
<tr>
<td><f:label path="name">Name:</f:label></td><td><f:input path="name" /></td><f:errors path="name"></f:errors>
</tr>
<tr>
<td><f:label path="email">Email:</f:label></td><td><f:input path="email" /></td><f:errors path="name"></f:errors>
</tr>
<tr>
<td><input type="submit" value="register" /></td>
</tr>
</table>
</f:form>
有什么建议吗?(我使用java 5,并且 javax.validation.Validator 不可用)
I'm having some issues finding out how to validate in spring 3.
What i have so far is this Java Bean....
public class User {
@NotEmpty
@Size(min=2, max=40)
private String name="";
@Email
private String email="";
public String getName(){
return name;
}
public String getEmail(){
return email;
}
public void setName(String name){
this.name=name;
}
public void setEmail(String email){
this.email=email;
}
}
This is the method that links to the page containing the form:
@RequestMapping(value="/form", method=RequestMethod.GET)
public String form(Model model){
model.addAttribute("user",new User());
return "form";
}
And this is the method in my controller, which "should" validate
@RequestMapping(value="/displayUser",method=RequestMethod.POST)
public String displayUser(Model model,@Valid User user,BindingResult result){
if(result.hasErrors()){
return "form";
}
userList.add(user);
model.addAttribute("user",user);
return "displayUser";
}
The @Validate annotation should make the validation, but in my case it does not.
Do you see something wrong in my code?
by the way this is the form i have "all is very simple, this is just for test purposes
<f:form method="post" action="displayUser" commandName="user">
<table align="center" border="1">
<tr>
<td>Your details</td>
</tr>
<tr>
<td><f:label path="name">Name:</f:label></td><td><f:input path="name" /></td><f:errors path="name"></f:errors>
</tr>
<tr>
<td><f:label path="email">Email:</f:label></td><td><f:input path="email" /></td><f:errors path="name"></f:errors>
</tr>
<tr>
<td><input type="submit" value="register" /></td>
</tr>
</table>
</f:form>
Any advise? (i use java 5 and there the javax.validation.Validator isn't available)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保添加javax.validation的validation-api和hibernate验证器。如果您使用 Maven,请在您的依赖项上添加以下内容:
在您的 applicationContext 上启用 Spring 注释驱动
Make sure to add javax.validation's validation-api and hibernate validator. If you use maven add the following on your dependencies:
Enable Spring annotation driven on your applicationContext