带有休眠验证的Spring MVC不起作用 [英] Spring MVC with hibernate validation doesn't work

查看:136
本文介绍了带有休眠验证的Spring MVC不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Spring进行休眠验证时遇到一些问题.我已经按照在线教程中的说明进行了所有操作,但是它不起作用,我只是转到下一页而没有验证错误.

I have some problems with hibernate validations with Spring. I did everything as explained in an online tutorial, but it's not working and I just go to the next page without validation error.

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Customer {

private String firstName;

@NotNull()
@Size(min=1, message = "this field must not to be empty")
private String lastName;

控制器:

@RequestMapping("/processForm")
public String processForm(@ModelAttribute("customer") @Valid Customer 
                          customer, BindingResult bindingResult) {
    if(bindingResult.hasErrors()) {
        return "customer-form";
    }
    return "customer-confirmation";
}

customer-form.jsp

customer-form.jsp

<form:form action="processForm.form" modelAttribute="customer">
    First name: <form:input path="firstName"/>
    <br>
    Last name (*): <form:input path="lastName"/>
    <form:errors path="lastName" cssClass="error"/>
    <input type="submit" value="Submit"/>
</form:form>

因此,当我的lastName为空字段时,BindingResult中没有错误.我在做什么错了?

So, there are no errors in BindingResult when I have an empty field for lastName. What am i doing wrong?

推荐答案

在类路径中添加hibernate-validator(如果尚不存在).如果您正在使用诸如gradlemaven之类的任何构建工具,只需将hibernate-validator添加到依赖项中即可.

Add hibernate-validator in your classpath if it does not exist already. If you are using any build tool like gradle or maven just add hibernate-validator to dependencies.

例如:

成绩:

compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.0.13.Final'

Maven:

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.13.Final</version>
</dependency>

这篇关于带有休眠验证的Spring MVC不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆