【问题标题】:Spring Mvc 3 hasErrors is always falseSpring Mvc 3 hasErrors 总是错误的
【发布时间】:2011-09-23 10:13:55
【问题描述】:

我有以下课程,

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 总是假的?为什么? 我需要任何进一步的配置吗?

【问题讨论】:

  • 您发送的数据是否会导致 MarketPlace 对象中的绑定错误?如果不是,hasErrors() 将始终正确地为假。
  • 是的,我正在为市场发送数据并且它们已正确绑定。只有验证没有触发
  • 将 mvc:annotation-driven 放在 xml 配置文件的开头。

标签: java jsp spring-mvc


【解决方案1】:

你必须在你的 spring-servlet 中设置注释“打开”,方法是添加:<mvc:annotation-driven/>

不要忘记还包括命名空间“mvc”。它看起来像这样:

<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">

【讨论】:

【解决方案2】:

仅将此添加到您的 spring XML 以启用对控制器的注释支持:

<mvc:annotation-driven/>

它应该在那之后工作

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-05-19
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 2011-02-21
  • 2015-01-15
相关资源
最近更新 更多