国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts源碼研究 - Bean-Message標簽篇

2019-11-18 15:56:04
字體:
來源:轉載
供稿:網友

  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對象的儲存)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浮梁县| 隆化县| 石台县| 克拉玛依市| 海南省| 武强县| 荣成市| 山西省| 尖扎县| 晋中市| 凤台县| 仪征市| 高淳县| 定日县| 修水县| 两当县| 固安县| 马关县| 武夷山市| 白城市| 微博| 崇礼县| 霍州市| 茶陵县| 旬邑县| 普定县| 平邑县| 砚山县| 旌德县| 鄢陵县| 石泉县| 广南县| 雷州市| 平凉市| 墨竹工卡县| 岫岩| 赤水市| 陆丰市| 韶山市| 来宾市| 孟村|