验证不适用于 Spring Boot 和 Hibernate

我是spring boot和hibernate的新手。我已经声明了一个模型类Office,它是:


package com.ashwin.officeproject.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.springframework.data.annotation.CreatedDate;

import org.springframework.data.annotation.LastModifiedDate;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;

import javax.validation.constraints.NotBlank;

import javax.validation.constraints.NotEmpty;

import javax.validation.constraints.NotNull;

import javax.validation.constraints.Size;


import java.util.Date;


@Entity

@Table(name = "office")

public class Office {


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long officeId;


    @NotEmpty(message = "{officename.notempty}")

    private String officeName;


    @NotNull

    private int officeNumber;


    /*@Size(min = 8, max = 72, message = "Your offc address between 8 and 72 characters long")*/

    /*@NotEmpty(message = "Please provide a offc name")*/

    private String officeAddress;


    public Office() {


    }


    //ommitted getters and setters

}

当我单击提交按钮时,如果我的表单中的输入字段为空,我想验证我的表单并在输入字段下方显示消息,例如“请提供有效的办公室名称”。但是发生的事情是当我点击提交按钮,然后它会转到我的 error.jsp 页面,但它没有显示我在上面声明的任何验证消息。我的错误页面显示是:


错误.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<p>Error</p>

</body>

</html>      

我在 Eclipse 中的控制台中也没有收到任何错误。当我的表单字段为空时,它只是将我重定向到 error.jsp 页面。


芜湖不芜
浏览 95回答 2
2回答

慕勒3428872

似乎 jar 冲突问题org.hibernate.validator:hibernate-validator:jar(javax.validation:validation-api:jar is part of hibernate validator)是 Spring Web 依赖项的一部分,因此无需添加可能发生冲突的额外依赖项,因此建议删除以下依赖项并通过全新安装(mvn clean install)再次构建项目。删除以下依赖项:<dependency>&nbsp; &nbsp; <groupId>org.hibernate</groupId>&nbsp; &nbsp; <artifactId>hibernate-validator</artifactId>&nbsp; &nbsp; <version>6.0.12.Final</version></dependency><dependency>&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; <artifactId>spring-boot-starter-validation</artifactId></dependency>并确保您访问下面的错误消息:不需要大括号和属性名称与您定义的实际消息属性名称匹配。(camelcase 与名称有关)。@NotEmpty(message = "officename.notempty")

慕尼黑8549860

我从 Spring Boot 2.2.4.RELEASE 迁移到 2.4.2,一旦我进行了更改,验证就会开始失败。开始失败的原因是从 Spring Boot 版本 2.3 中删除 web 模块的验证依赖项。我进行了以下更改以运行验证。&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-validation</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.hibernate</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>hibernate-validator</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${hibernate.validator}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.validation</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>validation-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${validation.api}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.logging</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jboss-logging</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${jboss.logging.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.fasterxml</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>classmate</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${fasterxml.classmate.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>相应地更改版本。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java