注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
Norton AntiVirus 2005测试版截图
让Ghostbusters为电脑保驾护航
安全小知识:Rundll.exe是病毒吗?
Htran把内网肉鸡做成SOCKS5代理
Windows Vista系统防火墙初探
微软官方解读Vista安全性: 全面的
Windows利器:Rundll.exe高级应用
DNS (domain Name System) 系统的
DNS 系统设定例--7.[Fwd] 特殊网
IMail Anti-Virus简易说明及快速
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
Struts源码研究 - Bean-Message标签篇
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:
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方法要求这个资源文件必须以ISO8859编码进行书写方能正确解析
所以,如果我们在资源文件中写入了中文,并在运行时出现了中文编码问题(?出现),那么只需确认您的资源文件
是否是以ISO8859为编码进行书写的即可
另外,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对象的储存)






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务