Spring MVC + Hibernate 验证器 +速度
我似乎无法使用 Spring MVC 进行基本验证。
我有以下 POJO:
public class Example {
@NotNull(message="You must enter a value")
@Size(min=2, max="2", message="You must enter 2 characters")
private String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
我非常基本的控制器如下所示:
@Controller
@RequestMapping("/value.vm")
public class ValueController {
@Autowired
private Validator validator;
@RequestMapping()
public ModelAndView display(@Valid @ModelAttribute("example") Example e, BindingResult result) {
//Details omitted
}
我的速度模板同样简单:
<form target="_self" enctype="application/x-www-form-urlencoded" action="/value.vm" method="post">
#springBind("example")
#springFormInput("example.value", "")
#springShowErrors("<br />", "")
</form>
我的 servlet.xml 文件包括:
<mvc:annotation-driven />
如果我手动验证空白表单提交(即 validator.validate(e)),我会看到正在生成我的错误消息,但它们从未存在于我的 BindingResult 中。
为什么我的绑定结果没有出现这些错误,有什么想法吗?
I can't seem to get basic validation working with Spring MVC.
I have the following POJO:
public class Example {
@NotNull(message="You must enter a value")
@Size(min=2, max="2", message="You must enter 2 characters")
private String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
My very basic controller looks like this:
@Controller
@RequestMapping("/value.vm")
public class ValueController {
@Autowired
private Validator validator;
@RequestMapping()
public ModelAndView display(@Valid @ModelAttribute("example") Example e, BindingResult result) {
//Details omitted
}
My velocity template is equally as simple:
<form target="_self" enctype="application/x-www-form-urlencoded" action="/value.vm" method="post">
#springBind("example")
#springFormInput("example.value", "")
#springShowErrors("<br />", "")
</form>
My servlet.xml file includes:
<mvc:annotation-driven />
If I manually validate a blank form submission (i.e. validator.validate(e)), I see that my error messages are being generated, however they never exist on my BindingResult.
Any ideas why my binding result is not getting these errors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我错误地使用:
当我需要使用
更改到此版本,并将验证代码修改为:
解决了我的问题。
I was mistakenly using:
When I needed to be using
Changing to this version, and modifying the validation code to:
solved my problem.
您仍然可以使用 JSR 303 @Valid 注释来使用 javax.validation.Validator 自动调用验证,而无需手动调用它。
如果您在应用程序上下文中包含
元素,或者在使用 Java 配置时使用 @EnableWebMvc,Spring 默认会启用此功能。为此,您需要有一个Bean Validation API 的实现,例如 Hibernate Validator
You can still use JSR 303 @Valid annotation to automatically invoke validation using javax.validation.Validator without invoking it manually.
This is enabled by default by spring if you include
<mvc:annotation-driven/>
element to your application context or @EnableWebMvc when you are using Java configFor this to work you need to have an implementation for Bean Validation API e.g Hibernate Validator