首页 > 解决方案 > 验证不工作 Spring Boot,Spring in Action 练习

问题描述

我正在跟随 Spring in Action 中的一章练习,该章通过验证,但即使我使用空输入发布表单,它也不会返回任何形式的验证错误!无论我做什么,我都无法触发验证错误响应,表单只是被接受,因为它完全是空的。

完整项目@https ://github.com/AdrianLarssonGit/tacoworld/tree/master/tacoworld

以下是我假设的相关代码:

订单控制器.java

package com.tacoworld;

import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tacoworld.TacoOrder;

@Slf4j
@Controller
@Validated
@RequestMapping("/orders")
public class OrderController {

    @GetMapping("/current")
    public String orderForm(Model model) {
        model.addAttribute("tacoOrder", new TacoOrder());
        return "orderForm";
    }
    
    @PostMapping
    public String processOrder(@Valid TacoOrder tacoOrder, Errors errors) {
        if(errors.hasErrors()) {
            return "orderForm";
        }
        else {
            log.info("Order submitted: " + tacoOrder);
            return "redirect:/";
        }
        
        
    }
}

orderForm.html

<!DOCTYPE html>
<html  xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>
Taco Cloud

</title>
<link th:href="@{style.css}" rel="stylesheet" />
</head>

<body>

<form method="POST" th:action="@{/orders}" th:object="${tacoOrder}">
<h1>Order your taco creation</h1>

<img th:src="@{/images/startSplash.jpg}" />
<br>
<a th:href="@{/design}" id="another">Design another taco!</a><br/>


<h3>Deliver my taco to...</h3>
<label for="deliveryName">Name: </label>
<input type="text" th:field="*{deliveryName}" />
<span class="validationError" th:if="${#fields.hasErrors('deliveryName')}" th:errors="*{deliveryName}">TEST Error</span>
<br/>
<label for="deliveryStreet">Street address: </label>
<input type="text" th:field="*{deliveryStreet}"/>
<br/>
<label for="deliveryCity">City: </label>
<input type="text" th:field="*{deliveryCity}"/>
<br/>
<label for="deliveryState">State: </label>
<input type="text" th:field="*{deliveryState}"/>
<br/>
<label for="deliveryZip">Zip code: </label>
<input type="text" th:field="*{deliveryZip}"/>
<br/>
<h3>Here's how I'll pay...</h3>
<label for="ccNumber">Credit Card #: </label>
<input type="text" th:field="*{ccNumber}"/>
<br/>
<label for="ccExpiration">Expiration: </label>
<input type="text" th:field="*{ccExpiration}"/>
<br/>
<label for="ccCVV">CVV: </label>
<input type="text" th:field="*{ccCVV}"/>
<br/>

<input type="submit" value="Submit order" />
</form>
</body>

TacoOrder.java

package com.tacoworld;

import java.util.List;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.CreditCardNumber;

import java.util.ArrayList;
import lombok.Data;


@Data
public class TacoOrder {
    @NotBlank(message="Delivery name is required")
    private String deliveryName;
    @NotBlank(message="Name must be at least 5 char long")
    private String deliveryStreet;
    @NotBlank(message="Name must be at least 5 char long")
    private String deliveryCity;
    @NotBlank(message="Name must be at least 5 char long")
    private String deliveryState;
    @NotBlank(message="Name must be at least 5 char long")
    private String deliveryZip;
    @CreditCardNumber(message="Name must be at least 5 char long")
    private String ccNumber;
    @Pattern(regexp="^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$",message="Wrong pattern MM/YY is needed")
    private String ccExpiration;
    @Digits(integer=3, fraction=0,message="Invalid CCV")
    private String ccCVV;
    
    private List<Taco> tacos = new ArrayList<>();

    
    public void addTaco(Taco taco) {
        this.tacos.add(taco);
        }
}

我正在使用 Spring Boot,所以除了尝试解决这个特定问题外,没有做太多的 POM 编辑,我还是将它附在此处以供参考:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tacoworld</groupId>
    <artifactId>tacoworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tacoworld</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>16</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
        <scope>provided</scope>
</dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        
        <dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
</dependency>
        
        
        <dependency>
   <groupId>org.hibernate.validator</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>6.0.13.Final</version>
</dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

标签: javaspringspring-boot

解决方案


删除整个 POM,关闭 Eclipse 然后通过 Spring Boot 重新生成 POM 似乎已经解决了这个问题。

不知道是 POM 还是 Eclipse 起作用了,但现在它确实起作用了。


推荐阅读