在 Struts2 的 struts.xml 中定義 Action 時,若省略特定屬性,框架會採用預設值。理解這些預設行為能簡化配置:
ActionSupport。execute() 方法。success。<package name="csdn" namespace="/test" extends="struts-default">
<action name="helloworld" class="cn.csdn.action.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
Struts2 提供多種結果類型,對應不同的視圖跳轉需求:
<!-- 同包下 -->
<result type="redirectAction">helloworld</result>
<!-- 跨命名空間 -->
<result type="redirectAction">
<param name="actionName">helloworld</param>
<param name="namespace">/test</param>
</result>
<!-- 支援 OGNL 表達式傳參 -->
<result type="redirect">view.jsp?id=${id}</result>
當多個 Action 需要返回相同視圖(如通用錯誤頁),可在 <package> 下定義 <global-results>:
<package name="demo" extends="struts-default">
<global-results>
<result name="message">/message.jsp</result>
</global-results>
</package>
Struts2 支援在配置文件中透過 <param> 為 Action 屬性注入值,前提是屬性必須提供 setter 方法:
public class HelloWorldAction {
private String savePath;
// getter/setter 省略
}
<action name="helloworld" class="cn.csdn.action.HelloWorldAction">
<param name="savePath">/images</param>
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
預設 Struts2 處理 *.action 請求,可透過常量 struts.action.extension 修改:
<constant name="struts.action.extension" value="do"/>
<!-- 多副檔名用逗號分隔 -->
<constant name="struts.action.extension" value="do,go"/>
常量可在 struts.xml 或 struts.properties 中定義,建議集中在 struts.xml 管理。載入順序(後者覆蓋前者):
struts-default.xmlstruts-plugin.xmlstruts.xmlstruts.propertiesweb.xml| 常量名稱 | 用途 | 建議值(開發/生產) |
|---|---|---|
| struts.i18n.encoding | 預設編碼集 | UTF-8 |
| struts.action.extension | 請求副檔名 | do,action |
| struts.serve.static.browserCache | 靜態資源瀏覽器快取 | false / true |
| struts.configuration.xml.reload | 配置檔熱重載 | true / false |
| struts.devMode | 開發模式(詳細錯誤) | true / false |
| struts.ui.theme | 預設 UI 主題 | simple |
| struts.objectFactory | Spring 整合工廠 | spring |
| struts.enable.DynamicMethodInvocation | 動態方法調用開關 | false(安全) |
| struts.multipart.maxSize | 上傳檔案大小限制 | 10701096 (10MB) |
大型專案建議按模組拆分配置檔,再用 <include> 引入:
<struts>
<include file="struts-user.xml"/>
<include file="struts-order.xml"/>
</struts>
// URL: /test/helloworld!other.action
public String other() { ... }
可透過 struts.enable.DynamicMethodInvocation=false 關閉以提升安全性。
<action name="helloworld_*" class="cn.csdn.action.HelloWorldAction" method="{1}">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
// 訪問 other():/test/helloworld_other.action
Action 定義同名屬性 + setter,Struts2 自動將請求參數轉型賦值。
// URL: /test/view.action?id=78
private Integer id; // 自動轉換 String → Integer
請求參數用 對象.屬性 格式,框架自動實例化對象並填充屬性。
// URL: /test/view.action?product.id=78
private Product product; // 自動 new Product() 並 setId(78)
HTTP 參數本質是字串,但業務邏輯需要豐富類型(日期、數字、集合、JavaBean)。Struts2 內建強大的類型轉換器,基於 OGNL 實現雙向轉換。
boolean, char, int, long, double, float 及其包裝類java.util.Date(依 Locale SHORT 格式)List、Map(元素預設為 String,可自定義轉換器)繼承 ognl.DefaultTypeConverter,實現 convertValue(Map context, Object value, Class toType):
getParameterValues())public class MyDateConverter extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
if (toType == Date.class) {
// String[] → Date
String[] vals = (String[]) value;
return DateFormat.getDateInstance(DateFormat.SHORT).parse(vals[0]);
} else if (toType == String.class) {
// Date → String
return DateFormat.getDateInstance(DateFormat.SHORT).format((Date) value);
}
return null;
}
}
註冊方式:在 xwork-conversion.properties 或 Action 同包下的 ActionName-conversion.properties 指定轉換器類名。