java - Spring Boot 应用程序表单未对给定的验证进行验证
问题描述
@Entity
public class People {
@NotNull
@Size(min = 2, max = 20)
private String name;
@NotNull(message = "Please enter Phone Number")
private long phoneNumber;
//getters and setters
}
对于上述模型类,我的 jsp 页面没有显示任何默认错误消息或提供给它的错误消息。表单正在以某种方式提交。我@valid
在发布路线中添加了注释。
另外,我添加了这个依赖项
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
发布路线
@Valid
@PostMapping("/emergency-complaint")
public String emergencyComplaintIndexPost(@Valid People people, BindingResult result) {
if (result.hasErrors()) {
return "emergency-complaint/index";
}
return "home";
}
表单 Jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CRS | Kolkata</title>
</head>
<body>
<form:errors path="people.*" cssStyle="color: #ff0000;" />
<div>
<h1>Lodge an Emergency Complaint Now!!</h1>
<form:form
action="/emergency-complaint"
method="post"
modelAttribute="people"
>
<form:label path="emergencyComplaint.complaint" for="complaint">
Emergency Complaint
</form:label>
<form:input
type="text"
name="complaint"
id="complaint"
path="emergencyComplaint.complaint"
/>
<form:label path="name" for="name">Name</form:label>
<form:input path="name" type="text" name="name" id="name" />
<form:label path="phoneNumber" for="phoneNumber"
>Phone Number</form:label
>
<form:input
path="phoneNumber"
type="Number"
name="phoneNumber"
id="phoneNumber"
/>
<button type="submit">Lodge</button>
</form:form>
</div>
</body>
</html>
POM.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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.3.1.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.naha</groupId>
<artifactId>crime-reporting-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- <dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解决方案
这validation-api
是Bean Validation
规范API,它不是实现。您需要在类路径中有一个实现(例如,Hibernate Validator)。
看到这个答案。
还需要@Valid
在这里放置您的处理程序方法。
推荐阅读
- spring-boot - Spring Boot Kafka - 与消费者不同的消息管理
- azure-billing-api - EA 使用计费中缺少 InvoiceId(或等效项)
- android-studio - Flutter build appbundle , debug-app.apk 工作正常,但 Flutter build apk 没有制作 apk 文件。这意味着什么?
- xml-parsing - 通过 Azure Logic 应用将 JSON 转换为 XML 格式
- import - 在verilog中,编译单元范围内的导入包和模块头之间有什么区别吗?
- selenium - 使用 Robot Framework 和 Excellibrary 将测试结果导出到 Excel
- javascript - 正则表达式 - 验证结束标签 - 模板引擎
- javascript - 使用基于 PHP 变量的 javascript 更改选择选项
- codenameone - 带有 GPS 位置标记的平面图
- php - 如何绕过用户''@'localhost'的访问被拒绝(使用密码:否)兄弟