<bean:message>标签用于输出Resource Bundle中的一条消息.<bean:message>标签的bundle属性指定Resource Bundle,它和Struts配置文件的<message:resource>元素的可以属性相匹配.如果没有设置bundle属性,就采用默认的Resource Bundle。
Beantaglibs应用的Struts配置文件(Struts-config.xml)中配置了两个Resource Bundle:
<message-resources parameter=”ApplicationResource” />
<message-resources parameter=”SpecialResouces” key=”special”/>
第一个Resource bundle 没有指定key属性,因此默认的Resource Bundle,它的资源文件ApplicationResource.properties,在这个文件中定义一条消息:
.hello=Hello,{0}
第二个Resource bundle指定key属性为”special”,它的资源文件为SpecialResources.properties,这个文件中定义了一条消息:
.hello=Hello,everyone!
在<bean:message>标签中指定消息key有三种方式:
1.<bean:message>标签的key属性直接指定消息key,例如:
<bean:message bundle=”special” key=”hello”/>
2.<bean:message>标签的name属性指定一个可以转化为字符串的变量,这个变量的字符串值为消息key,例如:
<%
//这个hello要对应到资源文件中的hello
Request.setAttribute(“StringBean”,”hello”);
%>
<bean:message bundle=”special” name=”stringBean”/>
3.同时指定<bean:message>标签的name属性和property属性.name属性指定一个JavaBean,property属性指定JavaBean的一个属性,这个JavaBean的属性的值就是消息的key,例如
<%
SomeBean bean = new SomeBean();
Bean.setName(“hello”);
Request.setAttribute(“someBean”,bean);
%>
<bean:message bundle=”special” name=”someBean” property=”name”/>
上面定义一个SomeBean类型的someBean变量,它的name属性为”hello”,因此消息的key为”hello”
对于带参数的复合消息,可以使用Bean:message标签中的arg0,arg1等属性来设置参数值
<bean:message key=”hello” arg0=”Linda”/>
资源文件ApplicationResources.properties
# Resources for parameter 'org.ybystruts.ApplicationResources'
# Project P/TagDemo
welcome = <h1>欢迎光临!!!!!!</h1>
welcome1 = <h1>欢迎 {0} 光临!!!!!!</h1>
JSP文件:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>beanWrite.jsp</title>
</head>
<body>
<!-- 通过bean:message的key能够找到属性文件{applicatinResource.properties}中的内容 -->
<bean:message key="welcome"/>
<!--bean:message标签中提供一个占位功能,
在输出的文件中占着一位,
这一位的数据等待标签填写 -->
<bean:message key="welcome1" arg0="yby"/>
<!-- <bean:message>标签的name属性指定一个可以转化为字符串的变量,
这个变量的字符串值为消息key -->
<%
request.setAttribute("testBean","welcome");
%>
<bean:message name="testBean"/>
<%
SimpleBean bean = new SimpleBean();
bean.setName("welcome");
request.setAttribute("someBean",bean);
%>
<bean:message name="someBean" property="name"/>
</body>
</html:html>
Beantaglibs应用的Struts配置文件(Struts-config.xml)中配置了两个Resource Bundle:
<message-resources parameter=”ApplicationResource” />
<message-resources parameter=”SpecialResouces” key=”special”/>
第一个Resource bundle 没有指定key属性,因此默认的Resource Bundle,它的资源文件ApplicationResource.properties,在这个文件中定义一条消息:
.hello=Hello,{0}
第二个Resource bundle指定key属性为”special”,它的资源文件为SpecialResources.properties,这个文件中定义了一条消息:
.hello=Hello,everyone!
在<bean:message>标签中指定消息key有三种方式:
1.<bean:message>标签的key属性直接指定消息key,例如:
<bean:message bundle=”special” key=”hello”/>
2.<bean:message>标签的name属性指定一个可以转化为字符串的变量,这个变量的字符串值为消息key,例如:
<%
//这个hello要对应到资源文件中的hello
Request.setAttribute(“StringBean”,”hello”);
%>
<bean:message bundle=”special” name=”stringBean”/>
3.同时指定<bean:message>标签的name属性和property属性.name属性指定一个JavaBean,property属性指定JavaBean的一个属性,这个JavaBean的属性的值就是消息的key,例如
<%
SomeBean bean = new SomeBean();
Bean.setName(“hello”);
Request.setAttribute(“someBean”,bean);
%>
<bean:message bundle=”special” name=”someBean” property=”name”/>
上面定义一个SomeBean类型的someBean变量,它的name属性为”hello”,因此消息的key为”hello”
对于带参数的复合消息,可以使用Bean:message标签中的arg0,arg1等属性来设置参数值
<bean:message key=”hello” arg0=”Linda”/>
资源文件ApplicationResources.properties
# Resources for parameter 'org.ybystruts.ApplicationResources'
# Project P/TagDemo
welcome = <h1>欢迎光临!!!!!!</h1>
welcome1 = <h1>欢迎 {0} 光临!!!!!!</h1>
JSP文件:
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<html:html lang="true">
<head>
<title>beanWrite.jsp</title>
</head>
<body>
<!-- 通过bean:message的key能够找到属性文件{applicatinResource.properties}中的内容 -->
<bean:message key="welcome"/>
<!--bean:message标签中提供一个占位功能,
在输出的文件中占着一位,
这一位的数据等待标签填写 -->
<bean:message key="welcome1" arg0="yby"/>
<!-- <bean:message>标签的name属性指定一个可以转化为字符串的变量,
这个变量的字符串值为消息key -->
<%
request.setAttribute("testBean","welcome");
%>
<bean:message name="testBean"/>
<%
SimpleBean bean = new SimpleBean();
bean.setName("welcome");
request.setAttribute("someBean",bean);
%>
<bean:message name="someBean" property="name"/>
</body>
</html:html>