bean:message的用法 以及标签的使用

<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>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值