spring mvc 验证消息参数

spring mvc validation meesage param

我正在使用 spring mvc 4,验证消息中的消息参数有问题。

例如@Size 的输出是:大小必须在最小值和最大值之间。 hibernate 验证器中的密钥包大小必须介于 {min} 和 {max} 之间。

这是我的 spring mvc 配置:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">





<context:component-scan base-package="net.devk"
    use-default-filters="false">

    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />

</context:component-scan>


<bean id="viewResolver"
    class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="checkRefresh" value="true"/>
    <property name="useMutableTilesContainer" value="true"/>
    <property name="definitions">
        <list>
            <value>/WEB-INF/views/**/view.xml</value>
            <value>/WEB-INF/layouts/layouts.xml</value>
        </list>
    </property>
</bean>

<!-- Configures the @Controller programming model -->
<!-- enables the support of annotation configuration for Spring MVC controllers, 
    as well as enabling Spring 3 type conversion and formatting support. Also, 
    support for JSR-303 Bean Validation is enabled by this tag -->
<mvc:annotation-driven validator="validator" />

<bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <property name="paramName" value="theme" />
</bean>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. 
    /?locale=de -->
<bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
    <property name="paramName" value="lang" />
</bean>


<!-- Configures Handler Interceptors -->
<mvc:interceptors>
    <ref bean="localeChangeInterceptor" />
    <ref bean="themeChangeInterceptor" />
</mvc:interceptors>

<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources/ directory -->
<!-- <mvc:resources mapping="/static/**" location="/static/" /> -->
<!-- defines the static resources (for example, CSS, JavaScript, images, 
    and so on) and their locations so Spring MVC can improve the performance 
    in serving those files -->
<mvc:resources location="/resources/" mapping="/resources/**" />

<!-- Enables the mapping of the DispatcherServlet to the web application’s 
    root context URL, while still allowing static resource requests to be handled 
    by the container's default servlet -->
<mvc:default-servlet-handler />


<!-- Saves a locale change using a cookie -->
<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
    <property name="cookieName" value="locale" />
</bean>


<bean
    class="org.springframework.ui.context.support.ResourceBundleThemeSource"
    id="themeSource">
    <property name="basenamePrefix" value="theme.theme-" />
</bean>
<bean class="org.springframework.web.servlet.theme.CookieThemeResolver"
    id="themeResolver">
    <property name="cookieName" value="theme" />
    <property name="defaultThemeName" value="default" />
</bean>


<bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource" ref="messageSource" />
</bean>

<bean
    class="org.springframework.web.multipart.support.StandardServletMultipartResolver"
    id="multipartResolver" />

<!-- Configure to plugin JSON as request and response in method handler -->
<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonMessageConverter" />
        </list>
    </property>
</bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />

问题出在哪里?

终于找到问题了... 我必须设置 messageSource.setUseCodeAsDefaultMessage(假); 然后将所有消息参数替换为正确的值!