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时类似,可以自己试试。
分享到:
相关推荐
NULL 博文链接:https://lygle.iteye.com/blog/857025
struts标签库 文章目录 bean:cookie 2 bean:define 3 bean:header 4 bean:include 5 bean:message 5 bean:page 7 bean:parameter 7 bean:resource 8 bean:size 8 bean:struts 9 bean:write 9 html:base 10 ...
Struts源码- Bean-Message标签,struts2使用资源文件
<td colspan="2"><bean:message key="login.page.title" /> <td><bean:message key="login.page.username" /> ...<html:errors property="org.apache.struts.action.GLOBAL_MESSAGE" /></form>
Struts 2 标签库(文档手册) Tags-API-CLSW-JSP <%@ taglib prefix="s" uri="/struts-tags" %> 就能使用struts2.0的标签库 下面就介绍每个标签的具体应用实例说明:按字母排列 A: 1. 2. <s:a href=""></s:a>-...
3、在jsp中采用<bean:message>标签来读取国际化消息文本 4、了解利用struts默认将locale放到session中的特性,完成采用编程的方式切换语言设置,设置语言到session中的,key为Globals.LOCALE_KEY,value为当前设置的...
Struts Logic标签库中包含的标签列表 Tag name Description empty 如果标签parameter,propertie等属性所指定的变量值为null或空字符串,则处理标签包含的内容 equal 如果标签parameter,propertie等属性所指定的...
从开发者角度看,就是说需要显示给用户的数据可以直接从Action中获取,而不像Struts1.x那样,必须把相应的Bean存到Page、Request或者Session中才能获取。Struts1.x 必须继承org.apache.struts.action.Action或者其...
16.2.7、国际化与<bean:message>标签 16.3、Logic标签 16.3.1、<logic:present>标签和<logic:notPresent>标签 16.3.2、<logic:empty>标签和<logic:notEmpty>标签 16.3.3、关系运算标签 16.3.4、<logic:...
16.2.7、国际化与<bean:message>标签 16.3、Logic标签 16.3.1、<logic:present>标签和<logic:notPresent>标签 16.3.2、<logic:empty>标签和<logic:notEmpty>标签 16.3.3、关系运算标签 16.3.4、<logic:iterate>...
<form-bean name="LoginForm" type="com.yza.struts.form.LoginForm"></form-bean> <action path="/login" name="LoginForm" type="com.yza.struts.action.LoginAction" scope="request" input="/...