最近打算將 Struts 2.0.x 版本直接升到 2.2.1,但如此一來之前為 Struts 2.0.x 所寫的一些能讓 XML 配置檔的 include tag 使用萬用字元的 Class 就無法運作了。

為了讓 Struts 2.2.1 XML 配置檔的 include tag 也能使用萬用字元,所以我又回頭去追原始碼,打算像之前 Struts 2.0.x 版一樣自己來寫一個,但是在追原始碼的過程中,我發現 Struts 2.2.1 本身已經有支援 include tag 使用萬用字元了,於是我在往前回溯到之前一度用過的 Struts 2.1.6,發現在 2.1.6 其實就已經有支援了,雖然我沒有再往前追查 2.1 最早的版本,但我相信 2.1 應該就已經支援了,而且不論如何,至少 2.1.6 確定是有支援配置檔的 include tag 使用萬用字元

Struts 2 有關讀入並解析 XML 配置檔的程式是在 com.opensymphony.xwork2.config.providers.XmlConfigurationProvider 類別,其中的 loadConfigurationFiles() 方法內可以看到解析 include tag 萬用字元的程式碼。

而負責解析萬用字元與取得的檔名之前是否相符是使用 XWork 的 com.opensymphony.xwork2.util.WildcardHelper 類別,以下是這個類別在處理萬用字元時的規則描述。

  • The '*' character is converted to MATCH_FILE, meaning that zero or more characters (excluding the path separator '/') are to be matched.
     
  • The '**' sequence is converted to MATCH_PATH, meaning that zero or more characters (including the path separator '/') are to be matched.
     
  • The '\' character is used as an escape sequence ('\*' is translated in '*', not in MATCH_FILE). If an exact '\' character is to be matched the source string must contain a '\\'. sequence.



最後照例補上一個範例,這個範例的寫法是假設我要載入所有在 classpath 中,不論其路徑為何,只要檔名是「 actions.xml 」的配置檔。

    <include file="**/actions.xml"/>

arrow
arrow
    全站熱搜

    大笨鳥 發表在 痞客邦 留言(0) 人氣()