为什么 @Valid 注释不适用于 Spring MVC 应用程序中的模型?
我正在研究 Spring MVC 并进行验证。
这是我的 build.gradle 文件。
plugins {
id 'java'
id 'war'
}
group 'org.example'
version '1.0-SNAPSHOT'
def hibernateVersion = '7.0.4.Final'
def junitVersion = '5.8.2'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:' + junitVersion
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:' + junitVersion
implementation 'org.springframework:spring-webmvc:5.3.16'
// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
compileOnly group: 'javax.servlet',
name: 'javax.servlet-api',
version: '4.0.1'
implementation 'javax.servlet:jstl:1.2'
implementation group: 'org.hibernate',
name: 'hibernate-validator',
version: hibernateVersion
implementation group: 'org.hibernate',
name: 'hibernate-validator-cdi',
version: hibernateVersion
testImplementation group: 'org.glassfish',
name: 'jakarta.el',
version: '4.0.1'
implementation group: 'javax.validation',
name: 'validation-api',
version: '2.0.1.Final'
implementation group: 'jakarta.el',
name: 'jakarta.el-api',
version: '4.0.0'
implementation group: 'javax.el',
name: 'javax.el-api',
version: '3.0.0'
}
test {
useJUnitPlatform()
}
这是我的控制器,在 custemerConfirmation 中我想验证 Customer
模型,但没有任何反应。
@Controller
@RequestMapping("/customer")
public class CustomerController {
@GetMapping("/form")
public String customerForm(Model model) {
model.addAttribute("customer", new Customer());
return "customer/form";
}
@GetMapping("/confirmation")
public String customerConfirmation(
@ModelAttribute("customer") @Valid Customer customer,
BindingResult bindingResult
) {
System.out.printf("\u001B[34m===Binding result: %s===\u001B[37m\n", bindingResult);
if (bindingResult.hasErrors()) {
return "customer/form";
}
customer.setFirstName(customer.getFirstName().toUpperCase());
customer.setLastName(customer.getLastName().toUpperCase());
return "customer/confirmation";
}
}
和我的 Customer
模型
public class Customer {
private String firstName;
@NotNull(message = "is required")
@NotEmpty(message = "is required")
@Length(min = 2, message = "Min length is 2 characters")
private String lastName;
public Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
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;
}
@Override
public String toString() {
return "Customer{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
经过验证的表单
<form:form method="get" action="/customer/confirmation" modelAttribute="customer">
First name: <form:input path="firstName"/>
<br><br>
Last name (*): <form:input path="lastName"/> <form:errors path="lastName" cssClass="field-error"/>
<br><br>
<input type="submit" value="Submit"/>
</form:form>
I studying Spring MVC and came to validation.
Here's my build.gradle file.
plugins {
id 'java'
id 'war'
}
group 'org.example'
version '1.0-SNAPSHOT'
def hibernateVersion = '7.0.4.Final'
def junitVersion = '5.8.2'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:' + junitVersion
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:' + junitVersion
implementation 'org.springframework:spring-webmvc:5.3.16'
// https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
compileOnly group: 'javax.servlet',
name: 'javax.servlet-api',
version: '4.0.1'
implementation 'javax.servlet:jstl:1.2'
implementation group: 'org.hibernate',
name: 'hibernate-validator',
version: hibernateVersion
implementation group: 'org.hibernate',
name: 'hibernate-validator-cdi',
version: hibernateVersion
testImplementation group: 'org.glassfish',
name: 'jakarta.el',
version: '4.0.1'
implementation group: 'javax.validation',
name: 'validation-api',
version: '2.0.1.Final'
implementation group: 'jakarta.el',
name: 'jakarta.el-api',
version: '4.0.0'
implementation group: 'javax.el',
name: 'javax.el-api',
version: '3.0.0'
}
test {
useJUnitPlatform()
}
Here's my controller and in custemerConfirmation I want to validate Customer
model but nothing happens.
@Controller
@RequestMapping("/customer")
public class CustomerController {
@GetMapping("/form")
public String customerForm(Model model) {
model.addAttribute("customer", new Customer());
return "customer/form";
}
@GetMapping("/confirmation")
public String customerConfirmation(
@ModelAttribute("customer") @Valid Customer customer,
BindingResult bindingResult
) {
System.out.printf("\u001B[34m===Binding result: %s===\u001B[37m\n", bindingResult);
if (bindingResult.hasErrors()) {
return "customer/form";
}
customer.setFirstName(customer.getFirstName().toUpperCase());
customer.setLastName(customer.getLastName().toUpperCase());
return "customer/confirmation";
}
}
And my Customer
model
public class Customer {
private String firstName;
@NotNull(message = "is required")
@NotEmpty(message = "is required")
@Length(min = 2, message = "Min length is 2 characters")
private String lastName;
public Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
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;
}
@Override
public String toString() {
return "Customer{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
The validated form
<form:form method="get" action="/customer/confirmation" modelAttribute="customer">
First name: <form:input path="firstName"/>
<br><br>
Last name (*): <form:input path="lastName"/> <form:errors path="lastName" cssClass="field-error"/>
<br><br>
<input type="submit" value="Submit"/>
</form:form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论