首页 > 解决方案 > 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>

标签: javaspringvalidationspring-mvcbean-validation

解决方案


validation-apiBean Validation规范API,它不是实现。您需要在类路径中有一个实现(例如,Hibernate Validator)。

看到这个答案。

还需要@Valid在这里放置您的处理程序方法。


推荐阅读