【问题标题】:Spring Boot Form Validation Not WorkingSpring Boot 表单验证不起作用
【发布时间】:2016-04-17 03:25:30
【问题描述】:

我是 Spring Boot 应用程序的新手。我自己搜索谷歌来学习 Spring Boot 应用程序。我在 CRUD 上为 Spring Boot 应用程序做了最简单的事情,但在 表单验证 方面遇到了一个问题。如果有人帮助我找出表单验证的问题,我在这里粘贴代码。

User.java

UserController.java

createUser.jsp

pom.xml

<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>SpringBootJSP</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-couchbase</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 您遇到了什么问题?仅发布代码而不提供有关无效内容的详细信息将无济于事。
  • 表单验证错误消息不显示在 from 或 jsp 页面上。

标签: maven spring-mvc spring-boot spring-form


【解决方案1】:

这一行:

model.addAtrribute("errors", bindingErrors.getAllErrors())

不需要。

如文档所述:

任何验证违规都将自动暴露为错误 BindingResult 可作为方法参数访问,也可渲染 在 Spring MVC HTML 视图中。

错误会自动暴露给视图。

【讨论】:

  • 你是对的,但是当我通过 bindingResult.hasErrors() 检查时,它会返回 false 作为结果,所以我无法获得验证违规。
猜你喜欢
  • 2020-11-22
  • 2020-11-05
  • 2021-05-08
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
相关资源
最近更新 更多