关于struts标签bean:message(转)
2009-12-07 10:52
369 查看
关于struts标签bean:message
bean:message标签用来从指定的locale中取回国际化的消息并输出,在这个过程中我们还可以传递五个以内的参数。message key可以通过key直接指定,也可以通过name和property间接的指定。 bean:message标签有两种指定message key的方式,一是通过key属性直接指定;二是通过name和property属性间接的指定,其中message key是在message resources文件中定义的。 我们可以在struts-config.xml文件中使用<message-resources>来设置message resources文件。 为了介绍该标签我使用了三个message resources文件,三个文件的名字分别为Resources.properties、Resources_en.properties和Resources_zh.properties。在struts-config.xml文件中的设置(这里不用设置三个,struts会依据locale自动找到对应的文件)如下: <message-resources parameter="Resources" /> 三个message resources文件中定义的message key为: <!-- Resources.properties --> resource=Resources.properties. from=Resources.properties. <!-- Resources_en.properties --> from=Resources_en.properties. <!-- Resources_zh.properties from=Resources_zh.properties. 下面的代码片段示例了bean:message标签的用法: <bean:message key="from"/><br/> <bean:message key="resource"/><br/> <html:link action="/locale?language=en">English</html:link> <html:link action="/locale?language=zh">Chinese</html:link> 上面的代码中含有改变locale的两个html:link标签,要使它们工作您的struts-config.xml文件中必须含有下面定义的form和action: <form-bean name="localeForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="language" type="java.lang.String" /> <form-property name="country" type="java.lang.String" /> <!--action成功后要跳到那里--> <form-property name="page" type="java.lang.String" initial="/message.jsp"/> </form-bean> <action path="/locale" type="org.apache.struts.actions.LocaleAction" name="localeForm" scope="request"> </action> 在不同的locale下我们得到了如下的两个结果: 在locale为zh时的结果: Resources_zh.properties. Resources.properties. 在locale为en时的结果: Resources_en.properties. Resources.properties. 让我们来看一下在locale为zh时如何得到的是上面的结果。因为locale为zh所以<bean:message key="from"/><br/>先找Resources_zh.properties这个文件从中得到form键的值。而<bean:message key="resource"/><br/>也会先找Resources_zh.properties这个文件但这次没有找到resource键,这时Struts会到Resources.properties这个文件中找,很幸运这里找到了。如果还没有找到,或message resource文件不存在就会抛出异常。当locale为en时类似,可以自己试试。 |
相关文章推荐
- 关于struts标签bean:message
- 关于struts标签bean:message(转)
- 关于struts标签bean:message(转)
- Struts源码研究 - Bean-Message标签篇
- struts中的bean-message标签用来实现国际化
- struts中的bean-message标签用来实现国际化
- Struts源码研究 - Bean-Message标签篇[转载]
- Struts源码研究 - Bean-Message标签篇
- Struts源码研究 - Bean-Message标签篇
- Struts源码研究-Bean-Message标签篇
- struts的bean标签使用message
- Struts 标签-bean
- Bean:message标签
- struts标签:bean标签
- struts标签库:bean:write struts-config.xml
- 关于struts标签checkbox
- 关于使用bean:write标签显示html内容
- Struts 2 Bean 标签教程
- Struts提供了五个标签库,即:HTML、Bean、Logic、Template和Nested。
- Struts的FormBean及常见错误和标签的使用