首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Struts2 國際化(i18n)與 OGNL 表達式語言完整教程

     編輯:Java 程式設計入門

一、資源文件命名規範

基礎名_語言_國家.properties
基礎名_語言.properties
基礎名.properties

// 例
csdn_zh_CN.properties   // 中國大陸簡體
csdn_en_US.properties   // 美國英語
csdn.properties         // 預設回退

中文屬性文件**必須**用 native2ascii 轉 Unicode 轉義:

native2ascii -encoding UTF-8 src.properties target.properties

二、全域資源配置與使用

<constant name="struts.custom.i18n.resources" value="csdn" />

JSP 頁面取值

<s:text name="welcome" />
<s:textfield name="realname" key="user" /> <!-- 表單標籤自動取 key -->

Action 中取值

public class MyAction extends ActionSupport {
    public String execute() {
        String msg = getText("welcome"); // 簡單 key
        String msg2 = getText("welcome", new String[]{"張三", "學習"}); // 含佔位符
    }
}

三、帶佔位符的國際化訊息

資源文件:welcome={0},歡迎來到{1}

<s:text name="welcome">
    <s:param><s:property value="realname"/></s:param>
    <s:param>學習</s:param>
</s:text>

四、包級資源文件

在 Java 包下放 package_zh_CN.properties,該包及子包 Action 自動訪問。**搜尋順序**:包級 → 全域常量指定資源。

五、Action 級資源文件

Action 類同目錄下放 ActionClassName_zh_CN.properties。**搜尋順序**:Action 級 → 向上包級 → 全域。

六、JSP 直接指定資源文件

<s:i18n name="csdn">
    <s:text name="welcome"/>
</s:i18n>

<!-- 包路徑寫法 -->
<s:i18n name="cn/csdn/action/package">
    <s:text name="welcome"><s:param>小張</s:param></s:text>
</s:i18n>

七、OGNL 表達式語言核心特性

OGNL(Object-Graph Navigation Language)是 Struts2 默認表達式引擎,強於 EL:

  • 支援**方法調用**:user.getFullName()
  • 支援**靜態方法/字段**:@java.lang.Math@PI、@com.Const@STATUS_OK
  • 支援**集合操作**:投影、選擇、Lambda 風格

八、OGNL 上下文結構與訪問規則

ActionContext (Map)
├── #application  → ServletContext
├── #session      → HttpSession
├── #request      → HttpServletRequest 屬性
├── #parameters   → Request 參數
├── #attr         → page→request→session→application 順序查找
└── root (ValueStack / OgnlValueStack)
    ├── [0] Action 實例(棧頂)
    ├── [1] OgnlUtil 等內部對象
    └── ...

OGNL 上下文結構圖

訪問語法

  • 根對象屬性:直接寫屬性名 username(從棧頂向下查找)。
  • 非根對象:加 # 前綴 #session.userId、#request['key']。

九、為何 EL 表達式能直接取 ValueStack 值

Struts2 用 StrutsRequestWrapper 包裝 HttpServletRequest,重寫 getAttribute(name):

public Object getAttribute(String name) {
    Object val = super.getAttribute(name); // 先找 request 屬性
    if (val == null && ActionContext.getContext() != null) {
        val = ActionContext.getContext().getValueStack().findValue(name); // 再找 ValueStack
    }
    return val;
}

因此 JSP 中 ${username} 實際走的是 request.getAttribute("username"),被攔截轉向 ValueStack。

十、OGNL 動態建構集合

<!-- 建構 List -->
<s:set name="list" value="{'zhangming','xiaoi','liming'}" />
<s:iterator value="#list" var="n">
    <s:property value="n"/><br/>
</s:iterator>

<!-- 建構 Map -->
<s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />
<s:iterator value="#foobar">
    <s:property value="key"/>=<s:property value="value"/><br/>
</s:iterator>

<s:set> 的 scope 屬性可指定存放範圍(application/session/request/page/action),預存入 OGNL Context。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved