Java 验证在spring引导和hibernate上不起作用
我不熟悉spring boot和hibernate。我声明了一个模型类Office,它是:Java 验证在spring引导和hibernate上不起作用,java,spring,hibernate,spring-boot,bean-validation,Java,Spring,Hibernate,Spring Boot,Bean Validation,我不熟悉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.Las
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
}
我已宣布我的第一个主要起始课程为:
package com.ashwin.officeproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
@SpringBootApplication
public class OfficeProjectApplication {
public static void main(String[] args) {
SpringApplication.run(OfficeProjectApplication.class, args);
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource
= new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public LocalValidatorFactoryBean getValidator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource());
return bean;
}
}
我的控制器类是:
package com.ashwin.officeproject.controller;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ashwin.officeproject.model.Office;
import com.ashwin.officeproject.repository.OfficeRepsitory;
@Controller
public class MainController {
@Autowired
OfficeRepsitory officeRepository;
@RequestMapping(value = { "/office"}, method = RequestMethod.GET)
public String office(Model model) {
model.addAttribute("offices", new Office());
return "office";
}
@RequestMapping(value = "/addOffice", method = RequestMethod.POST)
public String submit(@Valid @ModelAttribute("offices") Office office,
BindingResult result, ModelMap model) {
if (result.hasErrors()) {
return "error";
}
officeRepository.save(office);
model.addAttribute("offices", new Office());
return "office";
}
}
我有一个office.jsp页面,它是:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<jsp:include page="./header.jsp" />
<style>
.error {
color: #ff0000;
}
.errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
<form:form method="POST" action="/addOffice" modelAttribute="offices" >
<table>
<tr>
<td><form:label path="officeName">Name</form:label></td>
<td><form:input path="officeName"/></td>
<form:errors path = "officeName" cssClass = "error" />
</tr>
<tr>
<td><form:label path="officeNumber">Number</form:label></td>
<td><form:input path="officeNumber"/></td>
</tr>
<tr>
<td><form:label path="officeAddress">
Address</form:label></td>
<td><form:input path="officeAddress"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
<jsp:include page="./footer.jsp" />
.错误{
颜色:#ff0000;
}
.errorblock{
颜色:#000;
背景色:#ffEEEE;
边框:3倍实心#ff0000;
填充:8px;
利润率:16px;
}
名称
数
地址
我的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 http://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.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ashwin</groupId>
<artifactId>OfficeProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>OfficeProject</name>
<description>OfficeProject</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.1.3.1发布
com.ashwin
办公项目
0.0.1-快照
办公项目
办公项目
1.8
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
SpringBootStarterWeb
org.hibernate
休眠验证器
6.0.12.1最终版本
org.springframework.boot
弹簧启动启动器验证
org.springframework.boot
弹簧靴开发工具
运行时
mysql
mysql连接器java
运行时
org.springframework.boot
弹簧起动试验
测试
org.apache.tomcat.embed
汤姆卡特·贾斯珀
javax.servlet
jstl
org.springframework.boot
springbootmaven插件
单击“提交”按钮时,如果表单中的输入字段为空,我希望验证表单并在输入字段下方显示消息,如“请提供有效的办公室名称”。但发生的情况是,当我点击提交按钮时,它将进入我的error.jsp页面,但它没有显示我上面声明的任何验证消息。我的错误页面显示为:
error.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页面中重定向我 似乎jar冲突问题是因为
org.hibernate.validator:hibernate validator:jar(javax.validation:validation api:jar是hibernate validator的一部分)
是spring web依赖项的一部分,因此不需要添加可能冲突的额外依赖项,因此建议删除以下依赖项,并通过干净的安装重新构建项目(mvn清洁安装
)
删除以下依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.12.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
我从SpringBoot2.2.4.RELEASE迁移到了2.4.2,一旦做了更改,验证就开始失败 启动失败的原因是从SpringBootVersion2.3的web模块中删除验证依赖项 我做了以下更改以运行验证
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation.api}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>${jboss.logging.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>${fasterxml.classmate.version}</version>
</dependency>
org.springframework.boot
弹簧启动启动器验证
org.hibernate
休眠验证器
${hibernate.validator}
javax.validation
验证api
${validation.api}
org.jboss.logging
jboss日志
${jboss.logging.version}
com.fasterxml
同学
${fasterxml.classmate.version}
相应地更改版本。您是否可以尝试在
officeName
上添加@NotNull
?我认为@NotNull代表整数,这正是您配置它所要做的。如果出现错误,请显示error.jsp
。您应该返回office
模板以呈现模型错误,因为您已经在那里定义了这些错误(
)@Tijkiki我需要在我的代码中更改什么?我需要删除两个或其中一个?需要删除两个,因为它们是web依赖的一部分no bro如果你有teamviewer,你可以过来看看我的代码吗?抱歉,我不明白这个缩放链接是什么?从版本2.3.0开始发布Spring Boot web和WebFlux starters no longer取决于验证启动器,因此您必须将spring boot starter验证
添加到pom.xml中。有关详细信息,请查看2.3.0
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${validation.api}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>${jboss.logging.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>${fasterxml.classmate.version}</version>
</dependency>