---
title: "JSP_Struts标签 bean:message"
author: "shift8"
date: "2011-12-21T14:38:06Z"
tags: []
categories: ["Web前端"]
slug: "jsp-struts-bean-message"
draft: false
lastmod: "2011-12-21T14:55:32Z"
id: 1319524
url: "https://shift8.iteye.com/blog/1319524"
---

bean:message标签用来从指定的locale中取回国际化的消息并输出，在这个过程中我们还可以传递五个以内的参数。message key可以通过key直接指定，也可以通过name和property间接的指定。bean:message标签有两种指定message key的方式，一是通过key属性直接指定；二是通过name和property属性间接的指定，其中message key是在message resources文件中定义的。我们可以在struts-config.xml文件中使用&lt;message-resources&gt;来设置message resources文件。

一：标签知识

/\*\*\**

&lt;bean:message&gt;标签用于输出Resource Bundle中的一条消息.&lt;bean:message&gt;标签的bundle属性指定Resource Bundle,它和Struts配置文件的&lt;message:resource&gt;元素的可以属性相匹配.如果没有设置bundle属性,就采用默认的Resource Bundle。

Beantaglibs应用的Struts配置文件(Struts-config.xml)中配置了两个Resource Bundle:

              &lt;message-resources parameter=”ApplicationResource” /&gt;

              &lt;message-resources parameter=”SpecialResouces” key=”special”/&gt;

第一个Resource bundle 没有指定key属性,因此默认的Resource Bundle,它的资源文件ApplicationResource.properties,在这个文件中定义一条消息:

 .hello=Hello,{0}

第二个Resource bundle指定key属性为”special”,它的资源文件为SpecialResources.properties,这个文件中定义了一条消息:

 .hello=Hello,everyone!

在&lt;bean:message&gt;标签中指定消息key有三种方式:

1.&lt;bean:message&gt;标签的key属性直接指定消息key,例如:

   &lt;bean:message bundle=”special” key=”hello”/&gt;

2.&lt;bean:message&gt;标签的name属性指定一个可以转化为字符串的变量,这个变量的字符串值为消息key,例如:

&lt;%

              //这个hello要对应到资源文件中的hello

              Request.setAttribute(“StringBean”,”hello”);

%&gt;

&lt;bean:message bundle=”special” name=”stringBean”/&gt;

3.同时指定&lt;bean:message&gt;标签的name属性和property属性.name属性指定一个JavaBean,property属性指定JavaBean的一个属性,这个JavaBean的属性的值就是消息的key,例如

&lt;%

              SomeBean bean = new SomeBean();

              Bean.setName(“hello”);

              Request.setAttribute(“someBean”,bean);

%&gt;

&lt;bean:message bundle=”special” name=”someBean” property=”name”/&gt;

上面定义一个SomeBean类型的someBean变量,它的name属性为”hello”,因此消息的key为”hello”

对于带参数的复合消息,可以使用Bean:message标签中的arg0,arg1等属性来设置参数值

&lt;bean:message key=”hello” arg0=”Linda”/&gt;

资源文件ApplicationResources.properties

 # Resources for parameter 'org.ybystruts.ApplicationResources'

\# Project P/TagDemo

welcome = &lt;h1&gt;欢迎光临!!!!!!&lt;/h1&gt;

welcome1 = &lt;h1&gt;欢迎 {0} 光临!!!!!!&lt;/h1&gt;

JSP文件:

 &lt;%@ page language="java" contentType="text/html;charset=gb2312"%&gt;

&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.apache.org/tags-logic" prefix="logic" %&gt;

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

&lt;html:html lang="true"&gt;

  &lt;head&gt;

    &lt;title&gt;beanWrite.jsp&lt;/title&gt;

  &lt;/head&gt;

  &lt;body&gt;

  &lt;!-- 通过bean:message的key能够找到属性文件{applicatinResource.properties}中的内容 --&gt;

  &lt;bean:message key="welcome"/&gt;

  &lt;!--bean:message标签中提供一个占位功能,

    在输出的文件中占着一位,

    这一位的数据等待标签填写  --&gt;

  &lt;bean:message key="welcome1" arg0="yby"/&gt;

  &lt;!-- &lt;bean:message&gt;标签的name属性指定一个可以转化为字符串的变量,

           这个变量的字符串值为消息key --&gt;

  &lt;%

    request.setAttribute("testBean","welcome");

   %&gt;

   &lt;bean:message name="testBean"/&gt;

   &lt;%

  SimpleBean bean = new SimpleBean();

  bean.setName("welcome");

  request.setAttribute("someBean",bean);

 %&gt;

 &lt;bean:message  name="someBean" property="name"/&gt;

 &lt;/body&gt;

&lt;/html:html&gt;

\*/

二：国际化实例介绍

为了介绍该标签我使用了三个message resources文件，三个文件的名字分别为Resources.properties、Resources\_en.properties和Resources\_zh.properties。在struts-config.xml文件中的设置(这里不用设置三个，struts会依据locale自动找到对应的文件)如下：

&lt;message-resources parameter="Resources" /&gt;

三个message resources文件中定义的message key为：

&lt;!-- Resources.properties --&gt;

resource=Resources.properties.

from=Resources.properties.

&lt;!-- Resources\_en.properties --&gt;

from=Resources\_en.properties.

&lt;!-- Resources\_zh.properties 

from=Resources\_zh.properties.

下面的代码片段示例了bean:message标签的用法:

&lt;bean:message key="from"/&gt;&lt;br/&gt;

&lt;bean:message key="resource"/&gt;&lt;br/&gt;

&lt;html:link action="/locale?language=en"&gt;English&lt;/html:link&gt;

&lt;html:link action="/locale?language=zh"&gt;Chinese&lt;/html:link&gt;

上面的代码中含有改变locale的两个html:link标签，要使它们工作您的struts-config.xml文件中必须含有下面定义的form和action:

&lt;form-bean name="localeForm" 

type="org.apache.struts.action.DynaActionForm"&gt;

&lt;form-property name="language" type="java.lang.String" /&gt;

&lt;form-property name="country" type="java.lang.String" /&gt;

&lt;!--action成功后要跳到那里--&gt;

&lt;form-property name="page" type="java.lang.String" 

initial="/message.jsp"/&gt;

&lt;/form-bean&gt;

&lt;action path="/locale" type="org.apache.struts.actions.LocaleAction"

name="localeForm" scope="request"&gt;

&lt;/action&gt;

在不同的locale下我们得到了如下的两个结果：

在locale为zh时的结果：

Resources\_zh.properties.

Resources.properties.

在locale为en时的结果： 

Resources\_en.properties.

Resources.properties.

让我们来看一下在locale为zh时如何得到的是上面的结果。因为locale为zh所以&lt;bean:message key="from"/&gt;&lt;br/&gt;先找Resources\_zh.properties这个文件从中得到form键的值。而&lt;bean:messagekey="resource"/&gt;&lt;br/&gt;也会先找Resources\_zh.properties这个文件但这次没有找到resource键，这时Struts会到Resources.properties这个文件中找，很幸运这里找到了。如果还没有找到，或messageresource文件不存在就会抛出异常。当locale为en时类似，可以自己试试。

三：标签实现源代码研究

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

　　&lt;bean:message key="welcome.title"/&gt;

　　众所周知，这个标签做的事情是这样的：

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

　　&lt;message-resources parameter="resources.application"/&gt;

　　根据以上的定义，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() &gt; 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 &lt; 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对象的储存）