众所周知,这个标签做的事情是这样的:  访问在struts-config.xml中定义的资源文件,一般是application.properties,一般是这样定义的:    根">
 
网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  struts源码研究-bean-message标签篇     
  文章作者:未知  文章来源:水木森林  
  查看:79次  录入:管理员--2007-11-17  
 
  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对象的储存)
 
 
上一篇: struts‘stiles使用入门完全攻略    下一篇: eclipse走上榜首照亮java众生
  相关文档
如何将网页数据输出为Excel或Word文件 08-07
intellij idea 5.1 发布 11-17
jml起步--使用jml 改进你的java程序(3) 11-17
java从何而来 为什么如此火暴 11-16
spring2.0中新的bean类型实现原理 11-17
java教程:解析java的多线程机制 11-17
详细说明 midp 中的属性问题 11-17
java基础:mvc设计模式减少编程复杂性 11-16
java servlets技术 11-17
java框架:struts的动态复选框 11-17
课程介绍(13)sl-291 javabeans组件开发 11-16
osworkflow 小培训(1) 11-17
java人生:j2ee学习流程 11-17
java语言的calendar和date类 11-16
通过反射调用其他类的private method 01-09
剖析.net下的数据访问层技术(6) 11-17
用linenumberreader 提供更好的出错消息 11-17
web services&xml:xml和j2ee的完美结合 01-11
java学习之开始使用神奇的i=i++ 11-17
java基础类概述 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息