struts2基本配置

struts2框架的工作機制:

     * struts2框架提供了核心過濾器

     * 核心過濾器的作用是攔截請求資源

          *核心過濾器的生命週期:init(),doFilter(),destroy()

 

          * 在struts2框架運行時,將加載底層提供的各種配置文件

            * 啓動tomcat容器時,執行核心過濾器的init()方法

            * init()方法加載struts2框架底層提供struts-default.xmlstruts.xml等文件

 

          * 在攔截頁面請求資源時,將執行加載完的各種配置文件,進行相關的處理

            *當點擊頁面的請求連接時,執行核心過濾器的doFilter()方法進行攔截

            *在doFilter()方法裏執行init()方法加載完的struts-default.xml和struts.xml


struts2框架的工作流程:

* 把項目工程發佈到tomcat上

* tomcat容器加載web.xml配置文件

* 由於在web.xml配置文件中,配置了struts2框架的核心過濾器,加載其核心過濾器

* 在struts2框架提供的核心過濾器,解析struts.xml配置文件

* 核心的過濾器可以攔截頁面的請求資源,並在解析的struts.xml配置文件進行查找

*執行對應的類的對應的方法

* 然後通過執行對應類的對應方法的返回值,在解析的struts.xml配置文件找到對應的result類型

*進而轉向到對應的頁面

 

搭建struts2框架開發環境:

* 創建web工程

* 導入所需最少jar包

   struts2-core-2.3.1.1.jar:Struts 2框架的核心類庫

   xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基於xwork

   ognl-3.0.3.jar:對象圖導航語言(Object Graph Navigation Language),   struts2框架通過其讀寫對象的屬性           

   freemarker-2.3.18.jar:Struts 2的UI標籤的模板使用FreeMarker編寫

   commons-logging-1.1.x.jar:ASF出品的日誌包,Struts 2框架使用這個日誌 包來支持Log4J和JDK 1.4+的日誌記錄。                                         

  commons-fileupload-1.2.2.jar:文件上傳組件,2.1.6版本後需要加入此文件

   commons-io-2.0.1.jar:傳文件依賴的jar包

   commons-lang-2.5.jar:對java.lang包的增強

   asm-3.3.jar:提供了字節碼的讀寫的功能,包含了核心的功能,而其他的jar包都是基於這個核心的擴展.

   asm-commons-3.3.jar:提供了基於事件的表現形式。

  asm-tree-3.3.jar:提供了基於對象的表現形式。

  javassist-3.11.0.GA.jar:代碼生成工具, struts2用它在運行時擴展Java類

* 創建jsp頁面

* 創建action類文件

* 實現struts2框架提供的action接口,重寫execute()方法

*代碼如下:

publicclass HelloWorldAction implements Action {

publicString execute() throws Exception {

System.out.println("HelloWorldAction*********** execute()");

return"success";

}

}

 

* 創建struts2框架的配置文件,配置文件名爲"struts.xml":

<?xmlversion="1.0" encoding="UTF-8"?>

<!DOCTYPEstruts PUBLIC

"-//ApacheSoftware Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!--

頁面的請求連接:/primer/helloWorldAction.action

package:包

* name:包名,唯一的,必選項,不能爲空。用於被其他package繼承。

* namespace:命名空間,唯一的,可選項,缺省的時候是"/"。頁面的請求連接的前半部分。

*extends:繼承。

* extends="struts-default":struts-default.xml是由struts2框架底層提供的。

 -->

<packagename="primer" namespace="/primer"extends="struts-default">

<!--

action:

* name:action的名稱,對應的是頁面的請求連接的後半部分

*class:對應類的完整路徑

 -->

<actionname="helloWorldAction"class="cn.itcast.primer.HelloWorldAction">

<!--

result:結果類型

*name:返回的結果類型,對應的是執行類的方法的返回值

publicString execute() throws Exception {

System.out.println("HelloWorldAction*********** execute()");

return"success";

}

*後面的文本部分:要轉向到的頁面

 -->

<resultname="success">/primer/success.jsp</result>

</action>

</package>

<!--<package name="aaa" namespace="/aaa"extends="primer">

<actionname="abcdAction" class="cn.itcast.action.abcdAction">

<resultname="success">/success.jsp</result>

</action>

</package>-->

</struts>

 

* 在web.xml配置文件中,配置struts2框架提供的核心過濾器:

<filter>

         <filter-name>StrutsPrepareAndExecuteFilter</filter-name>

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

  </filter>

  <filter-mapping>

         <filter-name>StrutsPrepareAndExecuteFilter</filter-name>

         <url-pattern>/*</url-pattern>

  </filter-mapping>

 

 

struts2框架的攔截器:

* 由於struts2框架提供的核心過濾器的作用:處理請求資源的各種問題,導致核心過濾器過於龐大。

 

* 根據"分離關注"的概念,自定義多個過濾器,讓自定義的每一個過濾器只完成其中一個功能,核心過濾器只負責調用這些自定義過濾器。

 

* 自定義的過濾器定義爲"攔截器",攔截器是struts2框架底層提供的。在struts2框架中主要工作都是由攔截器來完成。

 

* struts2框架提供的攔截器,定義在struts-default.xml配置文件中。

<interceptorname="alias"class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>

        <interceptorname="autowiring"class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>

        <interceptor name="chain"class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>

        <interceptorname="conversionError"class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>

        <interceptor name="cookie"class="org.apache.struts2.interceptor.CookieInterceptor"/>

        <interceptorname="clearSession"class="org.apache.struts2.interceptor.ClearSessionInterceptor" />

        <interceptorname="createSession"class="org.apache.struts2.interceptor.CreateSessionInterceptor" />

        <interceptorname="debugging"class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor"/>

        <interceptorname="execAndWait"class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>

        <interceptorname="exception"class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

        <interceptorname="fileUpload"class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

        <interceptor name="i18n"class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>

        <interceptor name="logger"class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>

        <interceptorname="modelDriven"class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

        <interceptorname="scopedModelDriven"class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>

        <interceptor name="params"class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>

        <interceptorname="actionMappingParams"class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>

        <interceptorname="prepare"class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>

        <interceptorname="staticParams"class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

        <interceptor name="scope"class="org.apache.struts2.interceptor.ScopeInterceptor"/>

        <interceptorname="servletConfig"class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

        <interceptor name="timer"class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>

        <interceptor name="token"class="org.apache.struts2.interceptor.TokenInterceptor"/>

        <interceptorname="tokenSession"class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>

        <interceptorname="validation"class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>

        <interceptorname="workflow"class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>

        <interceptor name="store"class="org.apache.struts2.interceptor.MessageStoreInterceptor" />

        <interceptorname="checkbox"class="org.apache.struts2.interceptor.CheckboxInterceptor" />

        <interceptorname="profiling"class="org.apache.struts2.interceptor.ProfilingActivationInterceptor"/>

        <interceptor name="roles"class="org.apache.struts2.interceptor.RolesInterceptor" />

        <interceptorname="annotationWorkflow"class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor"/>

        <interceptorname="multiselect"class="org.apache.struts2.interceptor.MultiselectInterceptor" />

 

* 實際上,struts2框架提供的攔截器,就是各個具體類來完成。

 

* struts2框架在struts-default.xml配置文件中,提供了攔截器棧。來存放聲明好的攔截器。

 

*struts2框架使用攔截器棧,進而使用各種聲明好的攔截器。

 

* 攔截器的特點

<!--

               interceptor-stack:攔截器棧

                       *攔截器棧裏面存放的是上面聲明好的攔截器

                       *struts2框架是通過使用攔截器棧,進而使用對應的攔截器

                       *攔截器棧相當於list集合,執行攔截器棧時,是按照存放攔截器的先後順序來執行

                       -->

        <interceptor-stackname="defaultStack">

            <interceptor-refname="exception"/>

            <interceptor-refname="alias"/>

            <interceptor-refname="servletConfig"/>

            <interceptor-refname="i18n"/>

            <interceptor-refname="prepare"/>

            <interceptor-refname="chain"/>

            <interceptor-refname="scopedModelDriven"/>

            <interceptor-refname="modelDriven"/>

            <interceptor-refname="fileUpload"/>

            <interceptor-refname="checkbox"/>

            <interceptor-refname="multiselect"/>

            <interceptor-refname="staticParams"/>

            <interceptor-refname="actionMappingParams"/>

            <interceptor-refname="params">

                <paramname="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>

            </interceptor-ref>

            <interceptor-refname="conversionError"/>

            <interceptor-refname="validation">

                <paramname="excludeMethods">input,back,cancel,browse</param>

            </interceptor-ref>

            <interceptor-refname="workflow">

                <paramname="excludeMethods">input,back,cancel,browse</param>

            </interceptor-ref>

            <interceptor-refname="debugging"/>

        </interceptor-stack>

 

* 在struts-default.xml配置文件,定義了多個攔截器棧,那默認執行哪個攔截器棧?

<!-- 配置struts2框架運行時,默認執行哪個攔截器棧,defaultStack-->

        <default-interceptor-refname="defaultStack"/>

 

          <!-- 配置struts2框架運行時,默認執行哪個類 ActionSupport-->

        <default-class-refclass="com.opensymphony.xwork2.ActionSupport" />

   

    * 雖然struts2框架提供了很多攔截器,但是不能滿足所有需求。所以,還需要自定義攔截器

   

    * 過濾器和攔截器的區別:

      * 相同點:

         * 都是起攔截作用

 

      * 不同點:

         * 使用範圍:

             * 過濾器:屬於J2EE範疇,任何web工程都能使用

             * 攔截器:屬於struts2框架,離開struts2,攔截器就不能使用

         * 完成的功能:

             * 過濾器:只需要攔截頁面的請求資源

             * 攔截器:所有其他工作交給了攔截器來完成

         * 執行順序:過濾器——> 攔截器

       

       

struts2框架的基本配置:

* 關於struts2框架的頁面請求路徑:

*namespace+actionName+".action"

 

* 關於struts2對action的搜索順序:

1.獲得請求路徑的URI,例如url是:

      http://server/struts2/path1/path2/path3/test.action

2.首先尋找namespace爲/path1/path2/path3的package,

      如果存在這個package,則在這個package中尋找名字爲test的action,

      如果不存在這個package則轉步驟3;

3.尋找namespace爲/path1/path2的package,

      如果存在這個package,則在這個package中尋找名字爲test的action,

      如果不存在這個package,則轉步驟4;

4.尋找namespace爲/path1的package,

      如果存在這個package,則在這個package中尋找名字爲test的action,

      如果仍然不存在這個package,就去默認的namaspace的package下面去找名

      字爲test的action(默認的命名空間爲空字符串“/” ),

     如果還是找不到,頁面提示找不到action。

 

* 在沒有爲action指定class的時候:

<!--

* 沒有爲action指定class:在struts-default.xml文件進行配置

* 配置struts2框架運行時,默認執行哪個類 ActionSupport

                               <default-class-refclass="com.opensymphony.xwork2.ActionSupport" />

               * 沒有爲action指定執行的方法時,默認執行ActionSupport類下的execute()方法

                       publicString execute() throws Exception {

        return SUCCESS;

    }

* 沒有爲action指定返回類型,默認執行ActionSupport類下的execute()方法的返回值

 

 -->

<actionname="actionNoClass">

<resultname="success">/primer/success.jsp</result>

</action>

 

* 如果找不到對應的action的時候

* 會導致報錯:404 there is no Action map for ...

* 在struts.xml文件進行配置:

<!-- 在找不到對應action的時候,配置默認執行的action -->

<default-action-refname="helloWorldAction"></default-action-ref>

 

         *修改struts2框架的請求後綴名

                 *在struts.xml文件中進行配置:

                         *<constant name="struts.action.extension" value="do,love"></constant>

                         *相關常量在struts2框架底層提供的default.properties資源文件定義的

                 

                 *創建一個名爲"struts.properties"資源文件進行配置:

                         struts.action.extension=go

                 

                 *如果在struts.xml文件和struts.properties文件同時配置請求後綴名,struts.properties文件配置起作用的

                         *原因:如果常量在多個配置文件進行配置,struts2框架會有一個加載順序:

                                 *struts-default.xml

                                 *struts-plugin.xml

                                 *struts.xml

                                 *struts.properties

                                 *web.xml

         

         * 在struts.properties資源文件中配置的一些常量:

                 *<!--

配置當修改國際化資源文件時,是否重新加載

*false:默認值,不重新加載

*true:重新加載

 -->

<!--<constant name="struts.i18n.reload"value="true"></constant> -->

 

                 *<!--

當修改struts2框架的配置文件時,是否重新加載

*false:默認值,不重新加載

*true:重新加載

 -->

<!--<constant name="struts.configuration.xml.reload"value="true"></constant> -->

       

*<!--

配置struts2框架的模式

*false:是指生產模式

*true:是開發模式,具有更多的調試信息

###includes:

###- struts.i18n.reload = true

###- struts.configuration.xml.reload = true

 -->

<constantname="struts.devMode" value="true"></constant>

 

* 如果struts.xml配置文件過大時:可以自定義配置文件

*創建自定義的配置文件,一般一個模塊一個自定義配置文件

* 將自定義配置文件引入到struts.xml配置文件中:

<includefile="cn/itcast/primer/struts_primer.xml"></include>

 

 

 XMLCode By Object

<?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>

    <!-- 

        頁面的請求連接:/primer/helloWorldAction.action

        package:包

            * name:包名,唯一的,必選項,不能爲空。用於被其他package繼承。

            * namespace:命名空間,唯一的,可選項,缺省的時候是"/"。頁面的請求連接的前半部分。

            * extends:繼承。

                * extends="struts-default":struts-default.xml是由struts2框架底層提供的。

                    * 在struts框架底層提供struts-default.xml文件中聲明瞭所有的攔截器和攔截器棧,

                        來處理各種需求。我們需要繼承struts-defaulte.xml文件,進而使用這些攔截器來完成對應需求。

     -->

    <package name="primer" namespace="/primer" extends="struts-default">

        

        <!-- 在找不到對應action的時候,配置默認執行的action -->

        <default-action-ref name="helloWorldAction"></default-action-ref>

        

        <!-- 

            action:

                * name:action的名稱,對應的是頁面的請求連接的後半部分

                * class:對應類的完整路徑

         -->

        <action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction">

            <!-- 

                result:結果類型

                    * name:返回的結果類型,對應的是執行類的方法的返回值

                        public String execute() throws Exception {

                            System.out.println("HelloWorldAction *********** execute()");

                            return "success";

                        }

                    * 後面的文本部分:要轉向到的頁面

             -->

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

        </action>

        <!-- 

            * 沒有爲action指定class:在struts-default.xml文件進行配置

                * 配置struts2框架運行時,默認執行哪個類 ActionSupport

                    <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />

            * 沒有爲action指定執行的方法時,默認執行ActionSupport類下的execute()方法

                public String execute() throws Exception {

                    return SUCCESS;

                }

            * 沒有爲action指定返回類型,默認執行ActionSupport類下的execute()方法的返回值

        

         -->

        <action name="actionNoClass">

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

        </action>

    </package>

    

    <!-- <package name="aaa" namespace="/aaa" extends="primer">

        <action name="abcdAction" class="cn.itcast.action.abcdAction">

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

        </action>

    </package> -->

</struts>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章