【发布时间】:2021-06-29 06:50:36
【问题描述】:
验证没有完成,我找不到错误在哪里,即使我输入一个空字段,它也通过并且没有任何消息显示,大小也是同样的问题。 唯一有效的验证是“freePases”字段的最小值和最大值,它不允许我输入超过 10 位数字,但那里也没有显示任何消息,我希望验证确保数字介于 0 和10,不像现在那样包含 0 到 10 位数字。 我使用了休眠验证器 7 和验证 Api 2.0.1,并且使用了 IntelliJ Idea。 我不知道该怎么办,在此先感谢您的回答。
package com.spring.demo;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Customer {
private String firstName;
@NotNull(message="is required")
@Size(min=1, message="is required")
private String lastName;
@Min(value=0, message="must be greater than or equal to zero")
@Max(value = 10, message="must be less than or equal to 10")
private int freePasses;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getFreePasses() {
return freePasses;
}
public void setFreePasses(int freePasses) {
this.freePasses = freePasses;
}
}
package com.spring.demo;
import javax.validation.Valid;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}
@RequestMapping("/showForm")
public String showForm(Model theModel) {
theModel.addAttribute("customer", new Customer());
return "customer-form";
}
@RequestMapping("/processForm")
public String processForm(
@Valid @ModelAttribute("customer") Customer theCustomer,
BindingResult theBindingResult) {
//seeing if we have white spaces and if they passes validation
System.out.println("Last name: |" + theCustomer.getLastName() + "|");
if (theBindingResult.hasErrors()) {
return "customer-form";
}
else {
return "customer-confirmation";
}
}
}
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Customer Registration Form</title>
<style>
.error {color:red}
</style>
</head>
<body>
<i>Fill out the form, Asterisk (*) means required</i>
<br><br>
<form:form action="processForm" modelAttribute="customer">
First name :<form:input path="firstName"/>
<br><br>
Last name (*): <form:input path="lastName"/>
<form:errors paths="lastName" cssClass="error"/>
<br><br>
Free Passes (*): <form:input path="freePasses"/>
<form:errors paths="freePasses" cssClass="error"/>
<br><br>
<input type="submit" value="Submit">
</form:form>
</body>
</html>
【问题讨论】:
标签: spring hibernate validation spring-mvc intellij-idea