前言
struts.properties 是 Struts2 框架的核心配置文件,控制國際化、檔案上傳、靜態資源、動態方法調用、主題模板、URL 生成等核心行為。本文整理所有常用屬性,附預設值、說明與最佳實踐建議。
核心配置分類
1. 框架核心與擴充
| 屬性 | 預設值 | 說明 |
struts.configuration | org.apache.struts2.config.DefaultConfiguration | 配置管理器實作類,可自訂實作 Configuration 介面。 |
struts.objectFactory | spring | 物件工廠類,支援 spring、ejb 等,整合 IOC 容器。 |
struts.objectFactory.spring.autoWire | name | Spring 裝配模式:name、type、auto、constructor。 |
struts.objectFactory.spring.useClassCache | true | 整合 Spring 時是否快取 Bean 類別。 |
struts.objectTypeDeterminer | tiger / notiger | 類型判斷器,JDK 1.5+ 用 tiger。 |
2. 國際化與編碼
| 屬性 | 預設值 | 說明 |
struts.locale | en_US | 預設 Locale。 |
struts.i18n.encoding | UTF-8 | **強烈建議設為 UTF-8**,統一請求/回應/資源檔編碼。 |
struts.i18n.reload | false | 開發階段設 true,每次請求重新載入資源檔;生產環境 false。 |
struts.custom.i18n.resources | - | 自訂資源檔清單,逗號分隔(如 messages,errors)。 |
3. 檔案上傳
| 屬性 | 預設值 | 說明 |
struts.multipart.parser | jakarta | 解析器:jakarta (Commons FileUpload)、cos、pell。 |
struts.multipart.saveDir | javax.servlet.context.tempdir | 上傳暫存目錄。 |
struts.multipart.maxSize | 2097152 (2MB) | 單次上傳最大位元組數,超過拒絕。 |
4. 靜態資源服務
| 屬性 | 預設值 | 說明 |
struts.serve.static | true | true:從 JAR 服務靜態資源;false:需置於 <context_path>/struts。 |
struts.serve.static.browserCache | true | 瀏覽器快取靜態資源。開發 false,生產 true。 |
5. 動態方法調用與 URL
| 屬性 | 預設值 | 說明 |
struts.enable.DynamicMethodInvocation | true | 支援 action!method 語法。安全考量可關閉。 |
struts.enable.SlashesInActionNames | false | Action 名稱允許斜線(REST 風格)。 |
struts.action.extension | action | Action 映射副檔名,可多值逗號分隔。 |
struts.mapper.class | DefaultActionMapper | 自訂 URL-Action 映射器。 |
struts.url.http.port | 80 | HTTP 埠,生成 URL 用。 |
struts.url.https.port | 443 | HTTPS 埠。 |
struts.url.includeParams | get | 生成 URL 含參數:none、get、all。 |
struts.mapper.alwaysSelectFullNamespace | false | 命名空間選擇策略。 |
6. UI 主題與模板
| 屬性 | 預設值 | 說明 |
struts.ui.theme | xhtml | 預設主題:simple、xhtml、ajax。 |
struts.ui.templateDir | template | 模板目錄。 |
struts.ui.templateSuffix | ftl | 模板副檔名:ftl、vm、jsp。 |
7. 模板引擎配置
| 屬性 | 預設值 | 說明 |
struts.velocity.configfile | velocity.properties | Velocity 設定檔路徑。 |
struts.velocity.contexts | - | Velocity Context 設定。 |
struts.velocity.toolboxlocation | - | Toolbox 位置。 |
struts.freemarker.manager.classname | FreemarkerManager | FreeMarker 管理器類。 |
struts.freemarker.templatesCache | false | 模板快取。開發 false,生產 true(等同複製模板至 WEB_APP/templates)。 |
struts.freemarker.wrapper.altMap | true | 替代 Map 包裝。 |
struts.xslt.nocache | false | XSLT 樣式表快取。開發 true,生產 false。 |
8. 開發模式與熱重載
| 屬性 | 預設值 | 說明 |
struts.devMode | false | **開發階段必開 true**:詳細錯誤訊息、配置熱重載、OGNL 表達式除錯。 |
struts.configuration.xml.reload | false | struts.xml 變更時重新載入(devMode=true 時自動生效)。 |
9. 相容性修正
| 屬性 | 預設值 | 說明 |
struts.dispatcher.parametersWorkaround | false | WebLogic/Orion/OC4J 等無法處理 getParameterMap() 時設 true。 |
10. 自動載入配置文件
struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml
最佳實踐建議
- **開發環境**:
struts.devMode=true、struts.i18n.reload=true、struts.serve.static.browserCache=false、struts.freemarker.templatesCache=false、struts.xslt.nocache=true。
- **生產環境**:所有上述反轉,
struts.devMode=false 關鍵關閉。
- **編碼統一**:全專案強制
struts.i18n.encoding=UTF-8。
- **檔案上傳**:依業務調整
struts.multipart.maxSize,配合 Action 驗證。
- **安全性**:評估關閉
struts.enable.DynamicMethodInvocation、struts.enable.SlashesInActionNames。
結語
struts.properties 雖屬選配,但掌握其核心屬性能精準控制 Struts2 行為,避免預設值導致的開發/生產環境不一致問題。建議專案建立標準化 struts.properties 模板,納入版控管理。