首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

Struts 2 主題模板與國際化(i18n)完整配置指南

     編輯:MySQL 綜合教學

一、主題模板系統詳解

1.1 預設主題與生成的 HTML

Struts 2 預設使用 XHTML 主題。以 <s:textfield name="name" label="Name" /> 為例,生成的 HTML 結構為:

<tr>
  <td class="tdLabel">
    <label for="empinfo_name" class="label">Name:</label>
  </td>
  <td>
    <input type="text" name="name" value="" id="empinfo_name"/>
  </td>
</tr>

其中 empinfo 為 struts.xml 中定義的 action 名稱。

1.2 主題指定的六個層級(優先級遞減)

  1. 標籤屬性 theme="xhtml"
  2. 表單標籤屬性 theme
  3. Page 範圍屬性 theme
  4. Request 範圍屬性 theme
  5. Session 範圍屬性 theme
  6. Application 範圍屬性 theme
  7. 全域配置 struts.properties 中的 struts.ui.theme(預設 xhtml)

1.3 主題運作原理

每個 Struts 標籤對應一個模板文件(如 text.ftl, password.ftl),預設打包在 struts2-core-x.y.z.jar 中。

Struts 2 標籤 + 關聯模板文件 = 最終 HTML 標記碼

模板引擎預設為 FreeMarker(.ftl),亦可切換 Velocity (.vm) 或 JSP (.jsp),透過 struts.ui.templateSuffix 與 struts.ui.templateDir 配置。

1.4 自訂主題實戰步驟

  1. 複製現有主題:從 jar 解壓 template/xhtml 到 WebContent/WEB-INF/classes/template/mytheme
  2. 修改模板:編輯 control.ftl 簡化表單結構(如移除 table 包裹)
  3. 調整 form.ftl 引用自訂 control.ftl:<#include "/${parameters.templateDir}/mytheme/control.ftl" />
  4. 在 struts.properties 設定:
    struts.ui.theme=mytheme
    struts.ui.templateDir=template
    struts.ui.templateSuffix=ftl
  5. 打包 WAR 部署驗證

二、國際化(i18n)完整實作

2.1 資源包命名規則與搜尋順序

檔案命名:bundlename_language_country.properties(如 global_zh_TW.properties)

Struts 搜尋順序:

  1. ActionClass.properties
  2. Interface.properties
  3. SuperClass.properties
  4. model.properties
  5. package.properties
  6. struts.properties
  7. global.properties(最常用)

2.2 多語言資源包範例

# global.properties (預設 en_US)
global.name = Name
global.age = Age
global.submit = Submit
global.heading = Select Locale
global.success = Successfully authenticated

# global_zh_TW.properties
global.name = 姓名
global.age = 年齡
global.submit = 提交
global.heading = 選擇語系
global.success = 認證成功

# global_ja_JP.properties
global.name = 名前
global.age = 年齢
global.submit = 送信
global.heading = ロケール選択
global.success = 認証成功

2.3 視圖層訪問國際化訊息的四種方式

方式語法範例適用場景
<s:property><s:property value="getText('global.heading')" />任意位置輸出
<s:text><s:text name="global.heading" />簡單訊息顯示
<s:i18n><s:i18n name="some.package.bundle"><s:text name="key" /></s:i18n>指定資源包作用域
UI 標籤 key 屬性<s:textfield key="global.name" name="username"/>表單標籤自動取 label

2.4 完整範例:多語言切換表單

index.jsp - 語系切換連結與表單:

<s:url id="indexEN" namespace="/" action="locale">
  <s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{indexEN}">English</s:a>
<s:a href="%{indexES}">Spanish</s:a>
<s:a href="%{indexFR}">France</s:a>

<s:form action="empinfo" method="post" namespace="/">
  <s:textfield name="name" key="global.name" size="20" />
  <s:textfield name="age" key="global.age" size="20" />
  <s:submit name="submit" key="global.submit" />
</s:form>

Action 類別:

// 語系切換 Action
public class Locale extends ActionSupport {
  public String execute() { return SUCCESS; }
}

// 表單提交 Action
public class Employee extends ActionSupport {
  private String name;
  private int age;
  // getter/setter 省略
  public String execute() { return SUCCESS; }
}

struts.xml:

<constant name="struts.custom.i18n.resources" value="global" />
<package name="helloworld" extends="struts-default" namespace="/">
  <action name="locale" class="com.example.Locale">
    <result name="success">/index.jsp</result>
  </action>
  <action name="empinfo" class="com.example.Employee">
    <result name="success">/success.jsp</result>
  </action>
</package>

部署後訪問 https://localhost:8080/HelloWorldStruts2/index.jsp 即可體驗多語言切換效果。

三、總結

  • 主題模板系統提供強大的 UI 客製化能力,建議複製現有主題修改而非從零開始。
  • 國際化透過資源包 + 攔截器 + 標籤庫無縫整合,支援 Action、UI、錯誤訊息多層面本地化。
  • 實務上推薦使用 global.properties 統一管理應用層訊息,搭配 struts.custom.i18n.resources 全域載入。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved