Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表单验证是';我无法在我的帐户对象上进行验证_Java_Spring_Hibernate_Spring Mvc - Fatal编程技术网

Java 表单验证是';我无法在我的帐户对象上进行验证

Java 表单验证是';我无法在我的帐户对象上进行验证,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我的验证似乎不起作用,对hasErrors的调用不会返回false: if(bindingResult.hasErrors()) { ... } 以下是我目前的设置: 使用spring mvc,“我的模型”模块有一个Account类: @Entity @Table(name = "accounts") public class Account { @Id @GeneratedValue private int id; private int rootUse

我的验证似乎不起作用,对hasErrors的调用不会返回false:

if(bindingResult.hasErrors()) {
  ...
}
以下是我目前的设置:

使用spring mvc,“我的模型”模块有一个Account类:

@Entity
@Table(name = "accounts")
public class Account {

    @Id
    @GeneratedValue
    private int id;
    private int rootUserId;

    @Size(min=3, max=50, message="Username must be between 3 and 50 characters long.")
    private String name;

    ..
}
我的帐户管理员有一个创建操作,我的表单将发布到:

@RequestMapping(value = "/create", method = RequestMethod.POST)
    public ModelAndView create(@Valid Account account, BindingResult bindingResult) {

        ModelAndView mav = new ModelAndView("accounts/new");

        account.setName("a");

        if(bindingResult.hasErrors()) {
            return mav;
        }
        mav.setViewName("/test/");

        return mav;  // TODO should be redirecting, but not working b/c of MaV return value
    }
我目前没有在freemarker标记中使用任何表单帮助程序,当它到达控制器时,帐户尚未初始化。所以我现在只是手动将name属性设置为“a”,这应该会导致验证失败,但不会

我的bean(我想)连接在我的SessionFactorybean中:

<!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--<property name="packagesToScan" value="com.test.models" />-->
        <property name="configLocation" value="/WEB-INF/config/hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.query.substitutions=true 'Y', false 'N'
                hibernate.show_sql=true
                hibernate.hbm2ddl.auto=update

            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.test.models.Account</value>
            </list>
        </property>
    </bean>

应该是什么?

您是否在应用程序上下文中实例化了验证器?否实际的验证器。第6.7.2节但我通过注释添加验证,而不是自定义验证程序…困惑..阅读您的链接谢谢。您的注释是针对Hibernate的。您正在寻找的绑定结果验证通过InitBinder绑定。这里重要的是,您所进行的验证创建了SQL约束,因此您将在较低的级别进行扩展。如果你想在更高的层次上爆炸,你需要设置一个initbinder验证器来验证FOB。这还允许您设置错误消息,这样您就可以使用消息源并将它们吐回给用户。啊,所以我创建了一个验证程序bean:
现在它可以工作了,谢谢!
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0-rev-1</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
<input type="texT" ... name="account[name]" />
name="account.name"