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

Struts2 核心配置文件 struts.properties 完整屬性參考

     編輯:Java 相關知識

前言

struts.properties 是 Struts2 框架的核心配置文件,控制國際化、檔案上傳、靜態資源、動態方法調用、主題模板、URL 生成等核心行為。本文整理所有常用屬性,附預設值、說明與最佳實踐建議。

核心配置分類

1. 框架核心與擴充

屬性預設值說明
struts.configurationorg.apache.struts2.config.DefaultConfiguration配置管理器實作類,可自訂實作 Configuration 介面。
struts.objectFactoryspring物件工廠類,支援 spring、ejb 等,整合 IOC 容器。
struts.objectFactory.spring.autoWirenameSpring 裝配模式:name、type、auto、constructor。
struts.objectFactory.spring.useClassCachetrue整合 Spring 時是否快取 Bean 類別。
struts.objectTypeDeterminertiger / notiger類型判斷器,JDK 1.5+ 用 tiger。

2. 國際化與編碼

屬性預設值說明
struts.localeen_US預設 Locale。
struts.i18n.encodingUTF-8**強烈建議設為 UTF-8**,統一請求/回應/資源檔編碼。
struts.i18n.reloadfalse開發階段設 true,每次請求重新載入資源檔;生產環境 false。
struts.custom.i18n.resources-自訂資源檔清單,逗號分隔(如 messages,errors)。

3. 檔案上傳

屬性預設值說明
struts.multipart.parserjakarta解析器:jakarta (Commons FileUpload)、cos、pell。
struts.multipart.saveDirjavax.servlet.context.tempdir上傳暫存目錄。
struts.multipart.maxSize2097152 (2MB)單次上傳最大位元組數,超過拒絕。

4. 靜態資源服務

屬性預設值說明
struts.serve.statictruetrue:從 JAR 服務靜態資源;false:需置於 <context_path>/struts。
struts.serve.static.browserCachetrue瀏覽器快取靜態資源。開發 false,生產 true。

5. 動態方法調用與 URL

屬性預設值說明
struts.enable.DynamicMethodInvocationtrue支援 action!method 語法。安全考量可關閉。
struts.enable.SlashesInActionNamesfalseAction 名稱允許斜線(REST 風格)。
struts.action.extensionactionAction 映射副檔名,可多值逗號分隔。
struts.mapper.classDefaultActionMapper自訂 URL-Action 映射器。
struts.url.http.port80HTTP 埠,生成 URL 用。
struts.url.https.port443HTTPS 埠。
struts.url.includeParamsget生成 URL 含參數:none、get、all。
struts.mapper.alwaysSelectFullNamespacefalse命名空間選擇策略。

6. UI 主題與模板

屬性預設值說明
struts.ui.themexhtml預設主題:simple、xhtml、ajax。
struts.ui.templateDirtemplate模板目錄。
struts.ui.templateSuffixftl模板副檔名:ftl、vm、jsp。

7. 模板引擎配置

屬性預設值說明
struts.velocity.configfilevelocity.propertiesVelocity 設定檔路徑。
struts.velocity.contexts-Velocity Context 設定。
struts.velocity.toolboxlocation-Toolbox 位置。
struts.freemarker.manager.classnameFreemarkerManagerFreeMarker 管理器類。
struts.freemarker.templatesCachefalse模板快取。開發 false,生產 true(等同複製模板至 WEB_APP/templates)。
struts.freemarker.wrapper.altMaptrue替代 Map 包裝。
struts.xslt.nocachefalseXSLT 樣式表快取。開發 true,生產 false。

8. 開發模式與熱重載

屬性預設值說明
struts.devModefalse**開發階段必開 true**:詳細錯誤訊息、配置熱重載、OGNL 表達式除錯。
struts.configuration.xml.reloadfalsestruts.xml 變更時重新載入(devMode=true 時自動生效)。

9. 相容性修正

屬性預設值說明
struts.dispatcher.parametersWorkaroundfalseWebLogic/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 模板,納入版控管理。

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