gpt4 book ai didi

java - 如何验证表单页面中的整数输入?

转载 作者:行者123 更新时间:2023-12-02 00:49:47 25 4
gpt4 key购买 nike

我正在为客户创建页面,因此一切正常,直到我想强制用户在“免费通行证”字段中输入数字时出现错误

我尝试在 Customer 类中创建一个名为 freePasses 的整数类型字段,然后为其生成 getter 和 setter,我还修改了我的 .properties 文件

works fine

Error

客户.java

@Min(value = 0 , message="the value must be equal or greater than 0")
@Max(value = 10, message = "the value must be equal or less than 10")
@NotNull(message = "is required")
private Integer freePasses;

public Integer getFreePasses() {
return freePasses;
}



public void setFreePasses(Integer freePasses) {
this.freePasses = freePasses;
}

应用程序位置.属性

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
typeMismatch.customer.freePasses = invalid Number

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 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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</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-web</artifactId>
</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>
<version>8.5.20</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

无法将类型 java.lang.String 的属性值转换为属性 freePasses 所需的类型 java.lang.Integer;嵌套异常是 java.lang.NumberFormatException: For input string: "44xxxx"

最佳答案

我认为<form:input type=?number? path="date" />会起作用的。

关于java - 如何验证表单页面中的整数输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57869438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com