当前位置: 移动技术网 > IT编程>开发语言>Java > JSP Spring ApplicationContext的国际化支持

JSP Spring ApplicationContext的国际化支持

2017年12月08日  | 移动技术网IT编程  | 我要评论
jsp spring  applicationcontext的国际化支持 1.applicationcontext接口继承了messageresource接口,

jsp spring  applicationcontext的国际化支持

1.applicationcontext接口继承了messageresource接口,因此使用applicationcontext作为spring容器可以使用国际化资源文件。

2.在messageresource接口中定义了两个主要用于国际化的方法:

string getmessage(string param,object[] args,locale loc) ;

string getmessage(string param,object[] args,string default,locale loc) ;

applicationcontext就是通过这两个方法来完成国际化的

3.国际化支持原理:当程序创建applicationcontext时,spring会自动查找配置文件中名为messagesource的bean实例,如果找到这个实例,上面两个国际化方法的调用将会交给该bean实例实现类;如果没有找到,将会沿着父类一直往上找;如果最终还是没找到,系统将会创建一个空的staticmessagesource bean,该bean能接受上述两个方法的调用,但是是空实现。

4.通常在spring中使用resourcebundlemessagesource类来配置messagesource bean。配置该bean时需要提供一个参数basenames指定所使用的国际化资源文件的基本名,该参数值为list类型,因此需要使用元素来提供参数值

<bean name="messagesource" class="`org.springframework.context.support.resourcebundlemessagesource">
  <property name="basenames">
    <list>
      <value>message</value>
    </list>
  </property>
</bean>

<!--提供资源文件-->
//message_zh_cn.properties
welcome={0} . 欢迎光临!现在是北京时间:{1}
//message_en_us.properties
welcome={0} , welcome! now is:{1}

<!--在main程序中的调用-->
string name = act.getmessage("welcome",new string[]{"成兮”,new date()},locale.getdefault(locale.category.format)) ;

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网