java Spring portlet mvc:@Valid 似乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4283611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring portlet mvc: @Valid does not seem to work
提问by Hyman
I created a bean class and use it in my controller but it does not seem to work.
Namely even though I enter an invalid age, result.hasErrors
is still false.
我创建了一个 bean 类并在我的控制器中使用它,但它似乎不起作用。即即使我输入了一个无效的年龄,result.hasErrors
仍然是假的。
Bean class:
豆类:
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 snippet:
控制器片段:
@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:
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>
edit: My userRegistration-portlet.xml:
编辑:我的 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>
It's the mvc:spring-validation line that is giving me the: cvc-complex-type.2.4.c the matching wildcard is strict, but no declaration can be found for the element mvc:annotation-driven.
error.
这是给我的 mvc:spring-validation 行:cvc-complex-type.2.4.c the matching wildcard is strict, but no declaration can be found for the element mvc:annotation-driven.
错误。
采纳答案by Bozho
You need <mvc:annotation-driven />
to enable jsr-303 validation. If you don't want to use it for some reason, or if at some point it starts creating problems (like it did for me), take a look at this question
您需要<mvc:annotation-driven />
启用 jsr-303 验证。如果您出于某种原因不想使用它,或者在某个时候它开始产生问题(就像对我一样),请查看此问题
Update:
in schemaLocation
the mvc entry should contain these two:
更新:在schemaLocation
mvc 条目中应包含以下两个:
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
回答by heikkim
I had exactly the same problem. I read https://jira.springframework.org/browse/SPR-6817and worked the problem around by adding to my config:
我遇到了完全相同的问题。我阅读了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>
You also need to have a JSR-303 validator in your project in order for it to be availableFor this I used Hibernate Validator: http://www.hibernate.org/subprojects/validator.html
您还需要在您的项目中有一个 JSR-303 验证器才能使用它为此我使用了 Hibernate Validator:http: //www.hibernate.org/subprojects/validator.html
回答by Andy
You need to do the following, that is how I solved in the spring portlet mvc project.
需要做如下操作,我在spring portlet mvc项目中就是这样解决的。
Add
mvc:annotation-driven
to your applicatonContext.xmlAdd the following code in your controller:
@Autowired private LocalValidatorFactoryBean validator;
@InitBinder public void initBinder(WebDataBinder binder) { binder.setValidator(validator); }
添加
mvc:annotation-driven
到您的 applicationContext.xml在控制器中添加以下代码:
@Autowired private LocalValidatorFactoryBean validator;
@InitBinder public void initBinder(WebDataBinder binder) { binder.setValidator(validator); }
回答by dira
I think the reason is that ServletRequestDataBinder
(extends DataBinder) has no org.springframework.validation.Validator
instance, so it does not validate anything (to validate @Valid annotated bean you must have javax.validation.Validator instance).
我认为原因是ServletRequestDataBinder
(扩展 DataBinder)没有org.springframework.validation.Validator
实例,所以它不验证任何东西(要验证 @Valid 注释的 bean,你必须有 javax.validation.Validator 实例)。
org.springframework.validation.DataBinder
org.springframework.validation.DataBinder
public void validate() {
Validator validator = getValidator();
if (validator != null) {
validator.validate(getTarget(), getBindingResult());
}
}
You can set the Validator instance in @InitBinder
annotated methods:-
您可以在带@InitBinder
注释的方法中设置 Validator 实例:-
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.setValidator(new MyValidator());
}
Yo can also set the validator instance in WebBindingInitializer.
您还可以在WebBindingInitializer 中设置验证器实例。
StandardBindingInitializer
标准绑定初始化器
public class StandardBindingInitializer implements WebBindingInitializer {
@Autowired
private LocalValidatorFactoryBean validator;
public void setValidatorFactory(LocalValidatorFactoryBean validator) {
this.validator = validator;
}
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.setValidator(validator);
}
}
To create an instance of LocalValidatorFactoryBean either use mvc:annotation-drivenOR set up LocalValidatorFactoryBean as bean.
要创建 LocalValidatorFactoryBean 的实例,请使用mvc:annotation-driven或将 LocalValidatorFactoryBean 设置为 bean。