gpt4 book ai didi

java - @Valid 在 Spring java 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:51 27 4
gpt4 key购买 nike

我在Spring mvc中使用了@Valid。似乎什么也没做。这是我的程序

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.mvc"></context:component-scan>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

这是 Controller

 @RequestMapping(value="/test2")

public ModelAndView hello2(@Valid @ModelAttribute("person1") Person person1, BindingResult result){

if(result.hasErrors()){
ModelAndView model=new ModelAndView("form2");
return model;
}

ModelAndView model = new ModelAndView("test2");

return model;
}

Person.java这里我用过@NotNull@Size(min=5,max=30,message = "姓氏长度应为 5-30")

public class Person {
private String firstName;
@NotNull
@Size(min=5,max=30,message = "last name should be 5-30 long")
private String lastName;
private int age;

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mvc</groupId>
<artifactId>SpringMvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>


<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.0.Alpha2</version>
</dependency>
</dependencies>
</project>

最佳答案

寻找低于 6 的 Hibernate 版本并再次尝试,使用 @ModelAttribute 是可以将表单的输入映射到 Bean,并且 6.0 的 api 2.0 可能会导致有关 Bean 的一些问题。我建议您查看此主题以更清楚地了解此问题:::Spring annotations @ModelAttribute and @Valid

关于java - @Valid 在 Spring java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43819998/

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