`
shift8
  • 浏览: 147010 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

JSP_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文件。

一:标签知识

/****

<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>

*/

二:国际化实例介绍

为了介绍该标签我使用了三个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:messagekey="resource"/><br/>也会先找Resources_zh.properties这个文件但这次没有找到resource键,这时Struts会到Resources.properties这个文件中找,很幸运这里找到了。如果还没有找到,或messageresource文件不存在就会抛出异常。当locale为en时类似,可以自己试试。



三:标签实现源代码研究

Struts中非常常用的有这样的一个标签:


  <bean:message key="welcome.title"/>


  众所周知,这个标签做的事情是这样的:


  访问在struts-config.xml中定义的资源文件,一般是application.properties,一般是这样定义的:


  <message-resources parameter="resources.application"/>


  根据以上的定义,Struts将到WEB-INF/classes/resource/下去找application.properties文件,这是从以上配置信息的表面上看起来是这样,但通过查看Struts的源码,可以看到如下的代码:在org.apache.struts.util.PropertyMessageResources类中,有如下的代码:


  通过这段代码,可以看到


  A、.properties扩展名是写死在代码中的,所以资源文件必须使用这个扩展名


  B、Struts并不是单纯去寻找application.properties文件,而是首先找到application,赋给name变量然后加上下划线"_",然后再加上localeKey(如zh,en),然后再加上.properties


  C、确定了文件名之后,Struts使用了ClassLoader类的getResourceAsStream方法得到了一个InputStream


  D、然后Struts使用了java.util.Properties类的load方法,将资源文件中的所有资源读出放到了一个HashMap里面


  E、然后Struts就可以根据key值取出不同的message给前台了


  // Set up to load the property resource for this locale key, if we can


  String name = config.replace(’.’, ’/’);


  if (localeKey.length() > 0) {


  name += "_" + localeKey;


  }


  name += ".properties";


  InputStream is = null;


  Properties props = new Properties();


  // Load the specified property resource


  if (log.isTraceEnabled()) {


  log.trace(" Loading resource ’" + name + "’");


  }


  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();


  if (classLoader == null) {


  classLoader = this.getClass().getClassLoader();


  }


  is = classLoader.getResourceAsStream(name);


  if (is != null) {


  try {


  props.load(is);


  } catch (IOException e) {


  log.error("loadLocale()", e);


  } finally {


  try {


  is.close();


  } catch (IOException e) {


  log.error("loadLocale()", e);


  }


  }


  }


  D步骤中的load方法可以参看JDK的帮助文档,load方法要求这个资源文件必须以ISO885Array编码进行书写方能正确解析 所以,如果我们在资源文件中写入了中文,并在运行时出现了中文编码问题(?出现),那么只需确认您的资源文件是否是以ISO885Array为编码进行书写的即可


  另外,Struts在查找资源文件时,首先是按照如上的描述进行$Filename_$Locale.properties文件的查找如果他找不到,那么他就会用默认的$Filename.properties来找,如果还找不到,那就报错了这个Struts的查找顺序并不是我的杜撰,有如下Struts源码为证(这个方法是PropertyMessageResources.java中的):


  public String getMessage(Locale locale, String key) {


  if (log.isDebugEnabled()) {


  log.debug("getMessage(" + locale + "," + key + ")");


  }


  // Initialize variables we will require


  String localeKey = localeKey(locale);


  String originalKey = messageKey(localeKey, key);


  String messageKey = null;


  String message = null;


  int underscore = 0;


  boolean addIt = false; // Add if not found under the original key


  // Loop from specific to general Locales looking for this message


  while (true) {


  // Load this Locale’s messages if we have not done so yet


  loadLocale(localeKey);


  // Check if we have this key for the current locale key


  messageKey = messageKey(localeKey, key);


  synchronized (messages) {


  message = (String) messages.get(messageKey);


  if (message != null) {


  if (addIt) {


  messages.put(originalKey, message);


  }


  return (message);


  }


  }


  // Strip trailing modifiers to try a more general locale key


  addIt = true;


  underscore = localeKey.lastIndexOf("_");


  if (underscore < 0) {


  break;


  }


  localeKey = localeKey.substring(0, underscore);


  }


  // Try the default locale if the current locale is different


  if (!defaultLocale.equals(locale)) {


  localeKey = localeKey(defaultLocale);


  messageKey = messageKey(localeKey, key);


  loadLocale(localeKey);


  synchronized (messages) {


  message = (String) messages.get(messageKey);


  if (message != null) {


  messages.put(originalKey, message);


  return (message);


  }


  }


  }


  // As a last resort, try the default Locale


  这里可以看到Struts最后将localeKey赋空


  这样资源文件就是$Filename.properties了


  localeKey = "";


  messageKey = messageKey(localeKey, key);


  loadLocale这个方法将读取资源文件,填入HashMap


  这个方法的代码在上面已经列出来了


  loadLocale(localeKey);


  synchronized (messages) {


  message = (String) messages.get(messageKey);


  if (message != null) {


  messages.put(originalKey, message);


  return (message);


  }


  }


  // Return an appropriate error indication


  if (returnNull) {


  return (null);


  } else {


  return ("???" + messageKey(locale, key) + "???");


  }


  }


  至于这个$Locale的值是多少,通过很长时间的查找之后,发现了这样一些代码:


  在org.apache.struts.util.RequestUtils类中的600多行左右,有这样一个方法:


  public static Locale getUserLocale(HttpServletRequest request, String locale) {


  Locale userLocale = null;


  HttpSession session = request.getSession(false);


  if (locale == null) {


  locale = Globals.LOCALE_KEY; //这个值是org.apache.struts.action.LOCALE


  }


  // Only check session if sessions are enabled


  if (session != null) {


  userLocale = (Locale) session.getAttribute(locale);


  }


  if (userLocale == null) {


  // Returns Locale based on Accept-Language header or the server default


  userLocale = request.getLocale();


  }


  return userLocale;


  }


  可以看出,Struts将Locale的实例对象放到了session中,但是他什么时候将这个对象放到session里面的,尚未找到(很多配置在ActionServlet中读取并储存的,因为这个类是第一个启动的类,但这个类中没发现Locale对象的储存)

分享到:
评论

相关推荐

    一个利用struts做的简单国际化

    3、在jsp中采用&lt;bean:message&gt;标签来读取国际化消息文本 4、了解利用struts默认将locale放到session中的特性,完成采用编程的方式切换语言设置,设置语言到session中的,key为Globals.LOCALE_KEY,value为当前设置的...

    struts2 标签库 帮助文档

    Struts 2 标签库(文档手册) Tags-API-CLSW-JSP &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; 就能使用struts2.0的标签库 下面就介绍每个标签的具体应用实例说明:按字母排列 A: 1. 2. &lt;s:a href=""&gt;&lt;/s:a&gt;-...

    java web 视频、电子书、源码(李兴华老师出版)

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 上

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:iterate&gt;...

    李兴华 Java Web 开发实战经典_带源码_高清pdf 带书签 下

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:iterate&gt;...

    李兴华 java_web开发实战经典 源码 完整版收集共享

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:iterate&gt;...

    struts1 demo

    &lt;form-bean name="LoginForm" type="com.yza.struts.form.LoginForm"&gt;&lt;/form-bean&gt; &lt;action path="/login" name="LoginForm" type="com.yza.struts.action.LoginAction" scope="request" input="/...

    MLDN+李兴华+Java+Web开发实战经典.part3.rar )

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:...

    李兴华Java Web开发实战经典.pdf (高清版) Part1

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:...

    李兴华 Java Web 开发实战经典 高清扫描版Part3

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:iterate&gt;...

    李兴华Java Web开发实战经典(高清版) Part2

    16.2.7、国际化与&lt;bean:message&gt;标签 16.3、Logic标签 16.3.1、&lt;logic:present&gt;标签和&lt;logic:notPresent&gt;标签 16.3.2、&lt;logic:empty&gt;标签和&lt;logic:notEmpty&gt;标签 16.3.3、关系运算标签 16.3.4、&lt;logic:...

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    &lt;% @ taglib uri = " http://struts.apache.org/tags-bean " prefix = " bean " %&gt; &lt;% @ taglib uri = " http://struts.apache.org/tags-html " prefix = " html " %&gt; &lt;% @ taglib uri = " http://struts....

    struts基于mvc的开发代码

    &lt;!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" ... &lt;message-resources parameter="com.yourcompany.struts.ApplicationResources" /&gt; &lt;/struts-config&gt;

    struts2的入门开发

    &lt;form-bean name="loginForm" type="test.form.LoginForm"/&gt; − − &lt;action attribute="loginForm" input="/login.jsp" name="loginForm" path="/login" scope="request" type="test.action.LoginAction"&gt; ...

    Struts 成功测试

    INF\struts-config.xml 关键了,Struts配置文件 D:\web\WebRoot\WEB-INF\web.xml 配置文件,一般很少改动 =================================================================== Struts标签 D:...

    基于Hibernate和Struts的J2EE应用开发 PDF

    术:Enterprise Java Bean(EJB)、Java Servlets(Servlet)、Java Server Pages(JSP)、 Java Message Service(JMS)等,开发出了许多应用系统。但是,在传统J2EE 应用的开发过程中也出现了一些问题:1)数据模型和逻辑...

    SSH开发纪要整合解决四大问题(中文、jar包冲突、延时加载、模块化)文档

    &lt;form-bean name="addForm" type="com.lmf118.struts.form.AddForm" /&gt; attribute="addForm" input="/add.jsp" --错误时跳转的页面 name="addForm" --Frombean的名字 path="/add" --路径 ...

    从Java走向Java+EE+.rar

    9.4.3 Bean标签 134 9.4.4 Logic标签 136 9.4.5 Nested标签 137 9.5 小结 138 第10章 新一代的Struts 2 139 10.1 Struts 2框架的特点 140 10.2 实例——Struts 2的“Hello World” 143 10.3 小结 151...

    freemarker总结

    JAVA模版引擎Freemarker常用标签(一) 1. if指令 这是一个典型的分支控制指令,该指令的作用完全类似于Java语言中的if,if指令的语法格式如下: &lt;#if condition&gt;... &lt;#elseif condition&gt;... &lt;#elseif condition&gt;......

    外文翻译 stus MVC

    This article introduces Struts, a Model-View-Controller implementation that uses servlets and JavaServer Pages (JSP) technology. Struts can help you control change in your Web project and promote ...

Global site tag (gtag.js) - Google Analytics