J2EE走向成功路-01-Struts2 配置,j2ee-01-struts2

來源:互聯網
上載者:User

J2EE走向成功路-01-Struts2 配置,j2ee-01-struts2

配置環境

1.  匯入jar包,Struts2官網:http://struts.apache.org/

2.  建立Action類  繼承ActionSupport 

(ActionSupport類是一個工具類,它已經實現了Action介面。除此之外,它還實現了Validateable介面,提供了資料校正功能。通過繼承該ActionSupport類,可以簡化Struts 2的Action開發。

)

3.  Action配置:

1).在src檔案夾下建立struts.xml設定檔

2).匯入DTD檔案:

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

DTD檔案路徑:struts2-core-2.3.x.jar/struts-default.xml

3 ).建立根節點<struts></struts>

4 ).在根節點<struts>內建立<package>節點

<package>節點屬性:name="package名"

          extends="struts-default"(值固定)           

           namespace="/" (可不寫,預設值就是"/")

5 ).在<package>節點內建立<action>節點

<action>節點屬性:name="訪問名稱"(不需要加action,訪問的時候需要加上action,不能重複)

         class="實作類別的完整路徑";

         method="方法名"(預設執行execute方法)

以上配置一個 傳回值需要一個<action>,過於繁瑣,所以可以使用萬用字元:“ * ”

例如:<action name="test_*" class="全路徑名" method="{1}"></action> (name="test_*" 可以匹配到如test_a,test_b的訪問路徑,method="{1}" 花括弧裡面的1代表name中的第一個“ * ” 號)

6 ).在<action>節點下建立<result>(沒有傳回值不需要配置)

<result>節點屬性:name="方法的傳回值" (如果有傳回值,資料類型必須是String)

         type="dispatcher"(設定轉寄還是重新導向,預設是轉寄dispatcher,重新導向:redirect)

建立無傳回值的方法:方法無返回資料類型(void)

          02 return Action.NONE(NONE=NULL)

例如:<result name="success" type="dispatcher">index.jsp(跳轉頁面)</result>

7 ). 全域結果頁面配置:

<global-results>

<result name="success">/loding.jsp</result>

</global-results>

 

4. 在web.xml中配置過濾器

<!-- filter-class 在eclipse中 使用快速鍵:Ctrl+Shift+T 輸入:StrutsPrepareAndExecuteFilter  找到它的路徑 -->

         

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

以上就是Struts2的設定檔配置方法啦!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.