struts.properties 是 Struts 2 框架的核心配置檔案,通常置於 WEB-INF/classes 目錄下。它用於定義系統層級的基本慣例與預設行為。值得注意的是,所有在 struts.properties 中設定的屬性,均可在 struts.xml 或 web.xml(透過 Filter 初始化參數)中覆寫,三者優先級遞增。
struts.i18n.encoding=UTF-8
<constant name="struts.i18n.encoding" value="UTF-8" />
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
| 屬性名稱 | 說明 | 預設值 |
|---|---|---|
| struts.i18n.encoding | 請求/回應字元編碼 | ISO-8859-1 |
| struts.devMode | 開發模式(詳細錯誤訊息、熱重載) | false |
| struts.configuration.xml.reload | struts.xml 熱重載 | false |
| struts.mapper.alwaysSelectFullNamespace | 完整命名空間匹配 | false |
| struts.action.extension | Action 副檔名(預設 action,) | action, |
| struts.multipart.maxSize | 檔案上傳大小限制(位元組) | 2097152 (2MB) |
| struts.objectFactory | 物件工廠實作類別 | spring |
struts.properties,專案特定設定放在 struts.xml,部署環境差異(如資料庫連線)放在 web.xml 或外部化配置。struts.properties 應納入版本控制,但敏感資訊(密碼、金鑰)勿直接寫入,改用佔位符搭配外部配置中心。struts.devMode 開發環境設為 true,生產環境必須為 false,避免效能損耗與資訊洩露。struts.properties 是 Struts 2 系統行為的基石,掌握其與 struts.xml、web.xml 的關係與優先級,能讓配置管理更彈性、更清晰。實務上善用三層配置分工,可大幅降低維護成本。