Message Resources
Message Resources消息资源是Strus框架定义的一组消息资源包,用来简化开发、支持国际化的,开发者可以自由的将页面Title,Label Name,Error Message等显示信息定义在不同的资源文件中,然后在jsp、java中调用并显示,在需要更改语言时,只要更改应用的配置就可以实现调用不通的资源文件从而实现国际化。
2 如何设置应用系统的Message Resources?
有两种方式在应用系统中定义Message Resources,由于Struts支持模块化,所以在项目各小组并行开发的时候,可以构建不通Message Resources,当然每个项目组成员也可以为自己模块、包定义不同的Message Resources。
1、基于web.xml的配置:
配置文件如下:
<servlet> <servlet-name>action</servlet-name> <servlet-class>com.leppard.cppf.CPMaster</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>config/systemmanager</param-name> <param-value>/WEB-INF/struts-config-systemframe.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name> application </param-name> <param-value>ApplicationResources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> |
这个web配置文件中定义了两个模块,一个是应用主模块config,一个是子模块config/systemmanager。
参数application对应的是struts应用主模块的的资源配置文件,可以用“,”号隔开多个配置文件,如:
<init-param> <param-name> application </param-name> <param-value>ApplicationResources,ApplicationResources2</param-value> </init-param> |
资源配置文件格式如下:
message.message=Missing message for key {0} message.property=Property for message key must be a String message.resources=Missing resources attribute {0} page.selector=Invalid page context selector {0} label.systemmanager.index.title=txshine label.login.username=UserName label.login.password=Password |
另一种方法是在模块的配置文件/WEB-INF/struts-config.xml定义,如
<struts-config> <action-mappings> <action path="/exitAction" type="com.leppard.cppf.action.ExitAction"> <forward name="exit" path="exit.jsp" redirect="true" /> </action> </action-mappings> <message-resources key="myresource" parameter="resources.ApplicationResources_cn"/> </struts-config> |
Key是消息资源的模块名,parameter是消息资源的名称,resources.ApplicationResources_cn表示资源文件在WEB-INF/classes/resources目录下,文件名为ApplicationResources_cn。
3 如何引用Message Resources?
3.1 引用示例
<bean:message key="label.login.password"/> <bean:message bundle="myresource/systemmanager" key="label.systemmanager.index.title"/> <bean:message bundle="myresource" key="label.systemmanager.index.title"/> |
在jsp页面中引用Message Resources,首先要引入标签库
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
3.2 Message标签
message标签用来引用在Message Resources文件中定义的各种消息,message标签有以下几个属性:
1、Scrope属性,默认为PageContext.APPLICATION_SCOPE:范围见org.apache.struts.util. RequestUtils
scopes.put("page", new Integer(PageContext.PAGE_SCOPE)); 页面范围
scopes.put("request", new Integer(PageContext.REQUEST_SCOPE)); 请求范围
scopes.put("session", new Integer(PageContext.SESSION_SCOPE)); 会话范围
scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE)); 应用范围,资源配置文件中定义的message存在于整个应用中,所以在应用中的任何页面都可以引用到。
2、Local属性
本地属性或语言属性
3、name属性
名称,用来引用的Key
4、Bundle
模块名
5、arg[]
参数,传递给message消息的参数,动态定义的message,如
page.selector=Invalid page context selector {0}
通过传入arg[0]=index,显示出
Invalid page context selector index
3.3 不同模块的引用
1、主模块web.xml中配置的消息资源,在应用时不需要加bundle。
2、在主struts-config.xml主配置的消息资源,如果设定了资源的key属性,则在引用的时候需要指定bundle,如,
<message-resources key="myresource" parameter="resources.ApplicationResources_cn"/>
引用时
<bean:message bundle="myresource" key="label.systemmanager.index.title"/>
注意message的bundle和资源配置文件message-resources中的key一致
3、在子模块config/Sub
假设有在web.xml中指定了子模块如下:
<init-param>
<param-name>config/systemmanager</param-name>
<param-value>/WEB-INF/struts-config-systemframe.xml</param-value>
</init-param>
/WEB-INF/struts-config-systemframe.xml中指定了资源文件
<message-resources key="subresource" parameter="resources.subApplicationResources_cn"/>
在引用时的bundle就应该是资源文件模块名+应用子模块名,如
<bean:message bundle=" subresource / systemframe " key="label.systemmanager.index.title"/>