6
回答
org.hibernate.validator.constraints验证注解没找到message
【腾讯云】学生服务器套餐10元/月 >>>   

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;


/**
 *
 * @author hongshu @Mar 1, 2013
 */
public class Member implements Serializable {

 private static final long serialVersionUID = 8230208486954319424L;
 
 public Member(){
  super();
 }
 
 @Length(min=1, max=20, message="草拟大爷")
 private String name;
 
 @NotBlank(message = "{user.deletegroup.notempty}")
 private String description;

我在spring的controller写法是:

@RequestMapping(value = "/save-member.do", method = {RequestMethod.POST})
 public String saveMember(@ModelAttribute("member") @Valid Member member, BindingResult bindingResult, ModelMap modelMap) {
  if (bindingResult.hasErrors()) {
   modelMap.addAttribute("hasErrors", true);
   return "member/add-member";
  }
  System.out.println(member==null);
  return null;
 }

如果不符合vo里面的规则,会拦截下来,但是结果是下面这样的

 

我想让"**大爷" 配置到配置文件中去,

在spring的配置中是如下 写的:

<!-- 校验,注入国际化数据 -->
  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
   <property name="validationMessageSource" ref="messageSource" />
  </bean>
  
  <!-- 国际化配置 -->
  <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
   <property name="cacheSeconds" value="120" />
   <property name="defaultEncoding" value="UTF-8" />
   <property name="basenames">
    <list>
     <value>classpath:com/simple/xcode/i18n/framework.properties</value>
     <value>classpath:com/simple/xmodel/i18n/ui.properties</value>
     <value>classpath:com/simple/xmodel/i18n/userinfo.properties</value>
    </list>
   </property>
  </bean>

 

是我哪里写错了呢?求各位大神指教指教

举报
osrchina
发帖于5年前 6回/5K+阅
共有6个答案 最后回答: 2年前
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <property name="validationMessageSource" ref="messageSource"/> </bean>

<mvc:annotation-driven validator="validator"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:xxxx"/> <property name="defaultEncoding" value="UTF-8"/> <property name="useCodeAsDefaultMessage" value="true"/> </bean>

如果是:Spring3.x,则只能用HibernateValidator 4.x

Spring 3.x支持JSR303,HibernateValidator 4.x是JSR303的实现

可果是:Spring4.x,则能用HibernateValidator 5.x

Spring 4.x支持JSR349,HibernateValidator 5.x是JSR349的实现

以上配置是基于Spring3.x

另外:Spring3.x可不用显示配置HibernateValidator,它支持校验注解类名.ModelAtrribute(未配则为首字母小写的类).属性名 为key的消息模式,

例如:NotEmpty.employee.name=用户名不能为空

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        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/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
        http://www.springframework.org/schema/tx   
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <description>Spring-MVC配置文件</description>

    <!-- welcome page -->
    <mvc:view-controller path="/" view-name="main"/>
    <!-- 启用spring mvc 注解 -->
    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven validator="validator" conversion-service="conversion-service"/>


    <mvc:default-servlet-handler/>
    <!-- 扫描文件(自动将注解注入) -->
    <context:component-scan base-package="com.hoolai"/>
    <!-- 视图解析器策略 和 视图解析器 -->
    <!-- 1、此视图用于支持Servlet、JSP视图解析; -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包; -->
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <!-- 默认的viewClass,可以不用配置 <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> -->
        <!-- prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”; -->

        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->
    </bean>
    <!-- 静态文件处理 -->

    <mvc:resources mapping="/include/**" location="/include/" cache-period="36000"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html; charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>

            </list>
        </property>
    </bean>
    <!-- 拦截器 -->
    <!-- ResourceBundleViewResolver通过basename所指定的ResourceBundle解析视图名。 对每个待解析的视图,ResourceBundle里的[视图名].class所对应的值就是实现该视图的类。 同样,[视图名].url所对应的值是该视图所对应的URL。 可以指定一个parent view,其它的视图都可以从parent view扩展。 用这种方法,可以声明一个默认的视图。 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*.do"/>
            <mvc:mapping path="/*/*.do"/>
            <bean id="oauth2Interceptor" class="com.hoolai.weixin.interceptor.OAuth2Interceptor">
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>


    <!--国际化:在MVC配置文件中,配置国际化属性文件:-->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="basename" value="validate/message"/>
        <property name="cacheSeconds" value="120"/>
    </bean>


    <bean id="validateMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:validate/message"/>
        <property name="fileEncodings" value="utf-8"/>
        <property name="cacheSeconds" value="120"/>
    </bean>

    <bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"/>

    <!--数据验证-->
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
        <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
        <property name="validationMessageSource" ref="validateMessageSource"/>
    </bean>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    <!-- 缓存配置 -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
</beans>



顶部