| |
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对象的储存)
|
|