gpt4 book ai didi

java - Spring portlet mvc : @Valid does not seem to work

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:09:18 25 4
gpt4 key购买 nike

我创建了一个 bean 类并在我的 Controller 中使用它,但它似乎不起作用。也就是说,即使我输入了无效的年龄,result.hasErrors 仍然是错误的。

bean 类:

public class User{
@Min(13)
private int age;
private String name;

public int getAge() {
return age;
}

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

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}
}

Controller 片段:

@ActionMapping(params = "myAction=validateUser")
public void validateUser(ActionRequest request, ActionResponse response, ModelMap model, @ModelAttribute("user") @Valid User user, BindingResult result ){

if(result.hasErrors()){
for(ObjectError oe : result.getAllErrors()){
System.out.println(oe.getDefaultMessage());
}
} else{
//code
}
}

JSP:

<form:form action="${registerUser}" method="post" commandName="user">
<b>User</b>
<form:input path="age"/>
<form:input path="name"/>
<input type="submit" value="register"/>
</form:form>

编辑:我的 userRegistration-portlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/mvc
">

<mvc:annotation-driven />

<import resource="spring-hibernate.xml"/>

<context:component-scan base-package="comjohndoe.dao" />
<context:component-scan base-package="comjohndoe.model" />
<context:component-scan base-package="comjohndoe.service" />
<context:component-scan base-package="comjohndoe.util" />
<context:component-scan base-package="comjohndoecontroller" />

<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

是 mvc:spring-validation 行给我:cvc-complex-type.2.4.c 匹配的通配符是严格的,但是找不到元素 mvc:annotation-driven 的声明。 错误。

最佳答案

我遇到了完全相同的问题。我读了https://jira.springframework.org/browse/SPR-6817并通过添加到我的配置解决了这个问题:

<mvc:annotation-driven validator="validator" />

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<bean id="annotationMethodHandlerAdapter" class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean id="configurableWebBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator">
<ref bean="validator"/>
</property>
</bean>
</property>
</bean>

您的项目中还需要有一个 JSR-303 validator ,以便它可用 为此,我使用了 Hibernate validator :http://www.hibernate.org/subprojects/validator.html

关于java - Spring portlet mvc : @Valid does not seem to work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4283611/

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