我有以下课程,
public class MarketPlace {
@NotEmpty(message="This Template Name is required")
@Size(max=50)
private String templateName;
public String getTemplateName() {
return templateName;
}
}
使用以下post方法,
@RequestMapping(value = "/PublishApplication.htm", method = RequestMethod.POST)
public String PublishForm(@Valid MarketPlace m, BindingResult result, Map model) {
if (result.hasErrors()) {
return "PublishApplication";
}
return "PublishApplication";
}
但是hasErrors总是假的吗?为什么? 我需要进一步配置吗?
答案 0 :(得分:2)
你必须设置注释&#34; On&#34;在你的spring-servlet中添加:<mvc:annotation-driven/>
不要忘记还要包含命名空间&#34; mvc&#34;。它看起来像这样:
<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">
添加xlmns:mvc:
xmlns:mvc="http://www.springframework.org/schema/mvc
添加到xsi:schemaLocation:
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
答案 1 :(得分:0)
只将它添加到spring XML中以启用对控制器的注释支持:
<mvc:annotation-driven/>
它应该在那之后工作