Spring Mvc 3 hasErrors is always false
I have following class,
public class MarketPlace {
@NotEmpty(message="This Template Name is required")
@Size(max=50)
private String templateName;
public String getT开发者_如何转开发emplateName() {
return templateName;
}
}
With the following post method,
@RequestMapping(value = "/PublishApplication.htm", method = RequestMethod.POST)
public String PublishForm(@Valid MarketPlace m, BindingResult result, Map model) {
if (result.hasErrors()) {
return "PublishApplication";
}
return "PublishApplication";
}
But hasErrors is always false? why? Is I need any further configuration?
You have to set annotations "On" in your spring-servlet by adding this: <mvc:annotation-driven/>
Don't forget to include also the namespace "mvc". It'll look something like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
add xlmns:mvc:
xmlns:mvc="http://www.springframework.org/schema/mvc
add to xsi:schemaLocation:
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
Only add this to your spring XML to enable annotation support to controllers:
<mvc:annotation-driven/>
it should work after that
精彩评论