【问题标题】:Spring Boot BindingResult doesn't show any error messageSpring Boot BindingResult 不显示任何错误消息
【发布时间】:2021-02-27 18:46:41
【问题描述】:

在我的代码快照下方,当我输入不正确的值时,它不会向控制器显示任何错误消息

在执行此代码时,所有输入的数据都可以访问它,例如用户名为空或一个字符,电子邮件未显示正确的电子邮件类型。

如何解决我的数据验证问题?

控制器类

@Controller
@RequestMapping("/registration")
public class UserRegistrationController {

    @GetMapping
    public String showRegistrationForm(@ModelAttribute("userRegistrationDto")UserRegistrationDto userRegistrationDto) {
        return "registration";
    }
    
    @PostMapping
    public String registerUserAccount(@Valid @ModelAttribute("userRegistrationDto")  UserRegistrationDto userRegistrationDto, BindingResult result, Model model) {
        System.out.println(userRegistrationDto);
        model.addAttribute("data", userRegistrationDto);
        if(result.hasErrors()){
            System.out.println("error found");
            return "registration";
        }

UserRegistraionDto 类

public class UserRegistrationDto {

    @NotBlank
    private String name;
    @Size(min = 3, max = 15)
    private String username;
    @Email(message = "Email is not Valid")
    private String email;
    @Size(min = 9, max = 11, message = "phone number must be 11 digit")
    private String phone;
    private String address;
    private String password;

    public UserRegistrationDto(String name, String username, String email, String phone, String address, String password) {
        this.name = name;
        this.username = username;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.password = password;
    }
}

registration.html 页面

<div class="container regPage">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <!-- success message -->
            <div th:if="${param.success}">
                <div class="alert alert-info">You've successfully registered
                    to our awesome app!
                </div>
            </div>
            <div class="text-center sign">
                <h1>Registration Form</h1>
            </div>
            <form th:action="@{/registration}" method="post" th:object="${userRegistrationDto}">
                <div class="form-group">
                    <label class="control-label"  for="name"> Name </label>
                    <input id="name" name="name" class="form-control" th:field="*{name}"
                           required autofocus="autofocus"/>

                </div>
                <div class="form-group">
                    <label class="control-label" for="username"> User Name </label> <input
                        id="username"  name="username" class="form-control" th:field="*{username}"
                        required autofocus="autofocus"/>
                </div>
                <div class="form-group">
                    <label class="control-label" for="email"> Email </label> <input
                        id="email" name="email" class="form-control" th:field="*{email}" required
                        autofocus="autofocus"/>
                </div>
                <div class="form-group">
                    <label class="control-label" for="phone"> Phone </label> <input
                        id="phone" name="phone" class="form-control" th:field="*{phone}"
                        required autofocus="autofocus"/>
                </div>


                <div class="form-group">
                    <label class="control-label" for="address"> Address </label> <input
                        id="address" name="address" class="form-control" th:field="*{address}"
                        required autofocus="autofocus"/>
                </div>

                <div class="form-group">
                    <label class="control-label" for="password"> Password </label> <input
                        id="password" name="password" class="form-control" type="password"
                        th:field="*{password}" required autofocus="autofocus"/>
                </div>

                <div class="form-group text-center">
                    <button type="submit" class="btn btn-success">Register</button>
                </div>
            </form>
            <div class="text-center">
                <span>Already registered? <a href="/" th:href="@{/login}"> Login here</a></span>
            </div>
        </div>
    </div>
</div>

pom.xml 文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </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</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.0.Final</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

【问题讨论】:

  • @Valid 旁边添加@ModelAttribute 并从您的方法中删除模型并添加到模型中。你基本上是在覆盖东西。
  • 我试过这个,但这也显示了相同的@M.Deinum
  • 是的,我已经添加了@EnableWebMvc 应用内配置文件。 @托马斯
  • @Validated放在UserRegistrationController上面...
  • “模型模型”可能是问题 - 尝试将其删除。 Spring Boot 依赖于某种参数顺序。

标签: spring spring-boot spring-mvc spring-data-jpa


【解决方案1】:

删除:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>

【讨论】:

    【解决方案2】:

    如果您使用的是 spring boot,那么您可以使用 hibernate 验证器和验证 API 来代替

    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-validation</artifactId> 
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 2014-01-01
      • 2019-11-18
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多