@Validated注释不起作用

时间:2017-11-23 15:28:20

标签: java spring hibernate

我尝试使用@Validated注释来将有效的表单数据返回给控制器,但它无法调用我创建的自定义验证器。代码如下:

@PostMapping("/saveTransaction")
    public String saveTransaction(@ModelAttribute("transaction") 
@Validated Transactions transaction, BindingResult bindingResult) {

    if(bindingResult.hasErrors()) {
        System.out.println("Non formated form stuff.");
        return "transactions/transactionsForm";
    }

如果我手动调用Validator,则代码可以工作:

@PostMapping("/saveTransaction")
    public String saveTransaction(@ModelAttribute("transaction") Transactions transaction, BindingResult bindingResult) {
        validator.validate(transaction, bindingResult);
        if(bindingResult.hasErrors()) {
            System.out.println("Non formated form stuff.");
            return "transactions/transactionsForm";
        }

我正在使用以下代码自动连接我的类并初始化验证器。

    @Autowired
    private TransactionService transactionService;

    @Autowired
    @Qualifier("transactionValidator")
    private Validator validator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.setValidator(validator);
    }

I have the following code in Spring.xml file:

    <mvc:annotation-driven validator="transactionValidator" />

    <bean id="transactionValidator" class="org.lexusmanson.lexbudget.validator.TransactionValidator" />

我目前的依赖关系是:

   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.9</java.version>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <spring.version>5.0.1.RELEASE</spring.version>
        <hibernate.version>5.2.11.Final</hibernate.version>
        <hibernate.validator>5.4.1.Final</hibernate.validator>
        <c3p0.version>0.9.5.2</c3p0.version>
        <jstl.version>1.2.1</jstl.version>
        <tld.version>1.1.2</tld.version>
        <servlets.version>3.1.0</servlets.version>
        <jsp.version>2.3.1</jsp.version>
        <hsqldb.version>1.8.0.10</hsqldb.version>
        <aspectj.version>1.8.13</aspectj.version>
  </properties>

  <dependencies>
        <!-- Spring MVC Dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Hibernate Core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- Hibernate-C3P0 Integration -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>

        <!-- Hibernate Validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate.validator}</version>
        </dependency>

        <!-- JSTL Dependency -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>${tld.version}</version>
        </dependency>

        <!-- Servlet Dependency -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlets.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- JSP Dependency -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- HSQL Dependency -->
        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>${hsqldb.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>3.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
  </dependencies>

为什么赢取注释会调用我的验证码?

3 个答案:

答案 0 :(得分:2)

你可以尝试@valid annotation而不是@validated希望它会有所帮助。

答案 1 :(得分:1)

如下所述更改代码:绑定结果应在@Valid注释

之后
@PostMapping("/saveTransaction")
    public String saveTransaction(@Valid Transactions transaction, BindingResult bindingResult) {
        validator.validate(transaction, bindingResult);
        if(bindingResult.hasErrors()) {
            System.out.println("Non formated form stuff.");
            return "transactions/transactionsForm";
        }

答案 2 :(得分:0)

1:如果尚未添加,则添加以下依赖项

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.0.Final</version>
</dependency>

2:使用@Valid Annotation而不是@Validated annotation

3:在bean事务中使用一些验证约束,如@ NotEmpty,@ NotNull等

希望它适合你。