struts、spring国际化资源文件使用简介

来源:互联网 发布:安卓聊天软件编程 编辑:程序博客网 时间:2024/05/29 09:36

      前面记录了关于Java API来配置有使用国际化(i18n)的操作,这里将自己了解到的国际化操作在实际应用中的情况记录下来……

 

(一)、struts中的国际化简介:

1、配置struts-config.xml:在

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"><struts-config><form-beans><form-bean name="helloForm"type="org.lxh.struts.form.HelloForm" /></form-beans><global-exceptions /><global-forwards /><action-mappings><action attribute="helloForm" input="/hello.jsp"name="helloForm" path="/hello" scope="request"type="org.lxh.struts.action.HelloAction"><forward name="show" path="/hello.jsp"></forward></action></action-mappings><message-resources parameter="org.lxh.struts.ApplicationResources" /></struts-config>


注:以上配置的struts为版本1,在其后加入的message-resourses标记,属性parameter为文件所在的类路径。

2、编写一个简单的资源文件ApplicationResources.properties

# Resources for parameter 'com.xiaoli.struts.ApplicationResources'# Project StrutsFirsterror.info = \u8f93\u5165\u7684\u4fe1\u606f\u4e0d\u80fd\u4e3a\u7a7a\uff01msg.info = \u60a8\u8f93\u5165\u7684\u6570\u636e\u8fc7\u957f\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff01


3、使用

可在jsp页面通过struts标签:<bean:message key="error.info"/>,或者通过 new ActionMessage("msg.info")将其增加到ActionMessages中,而后Jsp页面有标签

<html:messages id="info" message="true">
  ${info}
 </html:messages>

得到资源文件的值。真正意义上的国际化资源文件在于将一个网站能够支持多国语言显示,比如一个网站提供两个链接选择使用那种语言显示(也可以是自动由浏览器识别)

参考:http://hi.baidu.com/liuzhe041/item/eda404e02b7fb5a4cf2d4fc0

 

(二)、sping 简单配置及使用

1、配置messagerResources.properties(略)

applicationContext.xml

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename" value="messagerResources" /></bean>


以上配置直接加到根结点<beans>下

 

2、使用,可以写一个方法封装

private static ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");

public static String getText(String code, Locale locale,Object... args){        String str = ctx.getMessage(code,args,locale);        return str;}/** *  * @param code * 国际化代码 * @rOeturn */public static String getText(String code){return getText(code, null, null);}

 

更多使用及配置还在学习之中……

 

 

原创粉丝点击