1. 程式人生 > >Struts2當中的struts.xml各個標籤都是什麼作用

Struts2當中的struts.xml各個標籤都是什麼作用

https://www.zhihu.com/question/38467318/answer/76528923
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!--
package: 包. struts2 使用 package 來組織模組. 便於查詢和維護,將相同模組的action請求放置在一起
name 屬性: 必須. 用於其它的包應用當前包.
extends: 當前包繼承哪個包, 繼承的, 即可以繼承其中的所有的配置. 通常情況下繼承 struts-default
struts-default 這個包在 struts-default.xml 檔案中定義.
namespace 可選, 如果它沒有給出, 則以 / 為預設值.
若 namespace 有一個非預設值, 則要想呼叫這個包裡的Action,
就必須把這個屬性所定義的名稱空間新增到有關的 URI 字串裡

http://localhost:8080/contextPath/namespace/actionName.action

-->
<package name="helloWorld" extends="struts-default">

<!--
配置一個 action: 一個 struts2 的請求就是一個 action
name: 對應一個 struts2 的請求的名字(或對一個 servletPath, 但去除 / 和副檔名), 不包含副檔名
class 的預設值為: com.opensymphony.xwork2.ActionSupport
method 的預設值為: execute
result: 結果.
-->
<action name="product-input"
class="com.opensymphony.xwork2.ActionSupport"
method="execute">
<!--
result: 結果. 表示 action 方法執行後可能返回的一個結果. 所以一個 action 節點可能會有多個 result 子節點.
多個 result 子節點使用 name 來區分
name: 標識一個 result. 和 action 方法的返回值對應. 預設值為 success
type: 表示結果的型別. 預設值為 dispatcher(轉發到結果.)
-->
<result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result>
</action>

<action name="product-save" class="com.atguigu.struts2.helloworld.Product"
method="save">
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>

<action name="test" class="com.atguigu.struts2.helloworld.Product" method="test">
<result>/index.jsp</result>
</action>

</package>

</struts>