com.baidu.unbiz.fluentvalidator.support
类 MessageSupport

java.lang.Object
  继承者 com.baidu.unbiz.fluentvalidator.support.MessageSupport

public class MessageSupport
extends Object

国际化使用的错误消息辅助类

使用MessageSource来作为delegate,利用Spring容器加载ResourceBundle,进行语言和国家地区的错误消息支持。

该类的使用方法如下:

1)新建一个properties文件,例如文件名为error-message.properties,内容为:

 car.size.exceed=car size exceeds
 
新建中文国际化文件error-message_zh_CN.properties,内容为:
     car.size.exceed=汽车数量超限
 

2)在Validator中的方法可以通过如下API调用获取错误消息:

     MessageSupport.getText("car.size.exceed");
 

3)在Spring的XML配置中加入如下配置即可:

 <bean id="messageSource"
 class="org.springframework.context.support.ResourceBundleMessageSource">
 <property name="basenames">
 <list>
 <value>error-message</value>
 </list>
 </property>
 </bean>

 <bean id="messageSupport"
 class="com.baidu.unbiz.fluentvalidator.support.MessageSupport">
 <property name="messageSource" ref="messageSource"/>
 </bean>
 

作者:
zhangxu

构造方法摘要
MessageSupport()
           
 
方法摘要
static String getText(String code)
          获取国际化消息
static String getText(String code, Object... args)
          获取国际化消息
static String getText(String code, Object[] args, Locale locale)
          获取国际化消息
 void prepare()
          如果在Spring容器中初始化,则打印一条消息
 void setLocale(String locale)
           
 void setMessageSource(org.springframework.context.MessageSource messageSource)
           
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

MessageSupport

public MessageSupport()
方法详细信息

prepare

@PostConstruct
public void prepare()
如果在Spring容器中初始化,则打印一条消息


getText

public static String getText(String code)
获取国际化消息

参数:
code - 消息key
返回:
消息

getText

public static String getText(String code,
                             Object... args)
获取国际化消息

参数:
code - 消息key
args - 参数列表
返回:
消息

getText

public static String getText(String code,
                             Object[] args,
                             Locale locale)
获取国际化消息

参数:
code - 消息key
args - 参数列表
locale - 语言地区
返回:
消息

setMessageSource

public void setMessageSource(org.springframework.context.MessageSource messageSource)

setLocale

public void setLocale(String locale)


Copyright © 2015–2017 neoremind. All rights reserved.