每個 <result> 元素擁有兩個核心屬性:
success。dispatcher(請求轉發)。Struts2 內建 10 種結果類型,定義於 struts-default.xml。
最常用類型,Server 端內部轉發,保留 request 作用域數據。
<!-- 寫法一:簡寫 -->
<result name="success">/manager/index.jsp</result>
<!-- 寫法二:完整參數 -->
<result name="success" type="dispatcher">
<param name="location">/manager/index.jsp</param>
</result>
location 參數對應 ServletDispatcherResult 原始碼中的位置屬性。
HTTP 302 重定向,瀏覽器重新發請求,**request 作用域數據丟失**,適合「Post-Redirect-Get」模式防止重複提交。
<result name="success" type="redirect">/login.jsp</result>
重定向到另一個 Action,支援兩種參數:
actionName:目標 Action 名稱(含 .action 後綴)namespace:目標命名空間,省略則沿用當前<!-- 方式一:簡寫 -->
<result name="success" type="redirectAction">resulttype/redirectactionAction.action</result>
<!-- 方式二:參數化 -->
<result name="success" type="redirectAction">
<param name="actionName">resulttype/redirectactionAction.action</param>
<!-- namespace 可選 -->
</result>
請求 URL:https://server/struts2/path1/path2/path3/test.action
namespace="/path1/path2/path3" 的 package/path1/path2、/path1""| 組件 | 模式 | 生命週期 |
|---|---|---|
| Struts2 Action | Prototype(原型) | **每次請求創建新實例**,線程安全 |
| Servlet | Singleton(單例) | 首次請求創建,後續共用,需自行處理線程安全 |
Servlet 生命週期:創建 → init() → service() → destroy()
