在 struts.properties 或 global.properties 中見到 key=${getText('some.key')},誤以為是 JSP EL 表達式 ${...},但 EL 僅能存取屬性 (getter),無法呼叫任意方法。
Struts 2 在載入國際化資源檔時,會對 ${...} 進行 OGNL 表達式解析,而非 EL。三種 OGNL 引用方式:
| 語法 | 用途 | 生效範圍 |
|---|---|---|
#{...} | 標籤屬性值、一般邏輯 | Struts 標籤內 (如 ` |
%{...} | 標籤字串屬性強制 OGNL 解析 | Struts 標籤屬性 (如 ` |
${...} | 國際化檔案、struts.xml 配置檔 | *.properties、struts.xml |
# global.properties
welcome.message = 歡迎, ${session.user.name}!
# 此處 ${} 會在資源檔載入時解析 OGNL,存取 ValueStack / Session
<action name="ListPhotos" class="com.example.PhotoAction">
<result>/photos.jsp?albumId=${albumId}</result>
</action>
# ${albumId} 會從 Action 實例取得 getAlbumId() 返回值
${...} 是標準 EL (JSP 2.4+),存取 Page/Request/Session/Application Scope 屬性。${...} 是 OGNL,可呼叫方法、存取 ValueStack、執行運算邏輯。{0}、{1},配合 `${...} 僅出現在 struts.xml、*.properties;JSP 統一用 EL 或 `搞清 ${...} 在不同檔案類型中代表的解析引擎 (EL vs OGNL),是精通 Struts 2 國際化與配置的關鍵認知。