国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Struts2學(xué)習筆記(二)——配置詳解

2019-11-15 00:47:51
字體:
供稿:網(wǎng)友
Struts2學(xué)習筆記(二)——配置詳解

1、Struts2配置文件加載順序:

  • default.PRoperties(默認常量配置)
  • struts-default.xml(默認配置文件,主要配置bean和攔截器)
  • struts-plugin.xml(配置插件)
  • struts.xml(配置action或者常量等)
  • struts.properties(常量配置)
  • web.xml(配置javaEE,如:監(jiān)聽器和過濾器)

2、Struts2配置文件詳解

1)default.properties

default.properties是Struts2的全局常量配置文件,default.default.properties的默認配置:

 1 //Struts2默認的編碼類型是UTF-8 2 struts.i18n.encoding=UTF-8 3 //指定jakarta為Struts的默認文件上傳包,即默認使用apache的fileupload組件。 4 struts.multipart.parser=jakarta 5 //上傳文件的最大字節(jié)數(shù) 6 struts.multipart.maxSize=2097152 7 //表單提交或者url請求時地址的后綴,如果需要指定多個請求后綴,則以英文逗號隔開 8 struts.action.extension=action,, 9 //Struts中action創(chuàng)建都是由對應(yīng)的工廠創(chuàng)建。10 struts.objectFactory = spring11 //指定spring框架的自動裝配類型,默認值為name,即默認根據(jù)bean的name屬性自動裝配12 struts.objectFactory.spring.autoWire = name13 //動態(tài)方法調(diào)用14 struts.enable.DynamicMethodInvocation = true15 //是否為開發(fā)模式16 struts.devMode = false17 //對于開發(fā)來講還是必將重要的。設(shè)置為true時,在每次請求時,資源包就會被重載。18 struts.i18n.reload=false19 //設(shè)置為true時,我們每次修改struts.xml文件后,框架會自動加載這個文件。20 struts.configuration.xml.reload=false21 //頁面使用靜態(tài)方法。通過ognl標簽調(diào)用值棧action中的方法。22 struts.ognl.allowStaticMethodaccess=false

default.properties是不能直接修改的,我們?nèi)绻薷?,有兩種方式:

  • 在src下創(chuàng)建struts.properties
    • 例如:struts.enable.DynamicMethodInvocation = true
  • 在struts.xml中配置(推薦使用)
    • 例如:<constant name="struts.devMode" value=”true” />

2)struts-default.xml

struts-default.xml文件是struts2框架默認加載的配置文件。它定義struts2一些核心的bean和攔截器。這些攔截器是以key-value對的形式配置在struts-default.xml中,其中name是攔截器名字,就是后面使用該攔截器的引用點,value則指定攔截器的實現(xiàn)類。

<?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框架底層提供出來的            * name:用于讓其他包來繼承的            * abstract:設(shè)置為抽象包,下面不能定義action標簽     -->    <package name="struts-default" abstract="true">        <!--             result-types:聲明結(jié)果類型                * name:結(jié)果類型的名稱                * class:結(jié)果類型對應(yīng)類的完整路徑                * default:設(shè)置其為默認,true是默認         -->        <result-types>            <!-- 轉(zhuǎn)發(fā)到action -->            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>            <!-- 轉(zhuǎn)發(fā)到jsp -->            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>            <!-- 重定向到j(luò)sp -->            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>            <!-- 重定向到action -->                                                          <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>            <!-- 用于下載 -->            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />        </result-types>        <!--             interceptors                * interceptor:聲明攔截器                    * name:攔截器的名稱                    * class:對應(yīng)攔截器類的完整路徑         -->        <interceptors>            <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>            <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>            <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>            <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>            <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>            <interceptor name="clearsession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />            <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />            <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />            <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>            <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>            <interceptor name="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"/>            <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>            <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>            <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>            <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>            <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>            <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>            <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>            <interceptor name="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"/>            <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>            <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>            <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>            <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />            <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />            <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />            <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />            <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />            <interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />                        <!-- 聲明自定義攔截器 -->            <interceptor name="expessionInterceptor" class="cn.itcast.aop.ExpessionInterceptor"></interceptor>            <!-- Basic stack -->            <interceptor-stack name="basicStack">                <interceptor-ref name="exception"/>                <interceptor-ref name="servletConfig"/>                <interceptor-ref name="prepare"/>                <interceptor-ref name="checkbox"/>                <interceptor-ref name="multiselect"/>                <interceptor-ref name="actionMappingParams"/>                <interceptor-ref name="params">                    <param name="excludeParams">dojo/..*,^struts/..*,^session/..*,^request/..*,^application/..*,^servlet(Request|Response)/..*,parameters/...*</param>                </interceptor-ref>                <interceptor-ref name="conversionError"/>            </interceptor-stack>            <!--                 interceptor-stack:攔截器棧                    * struts2框架通過使用攔截器棧,進而使用上面聲明好的攔截器                    * 在攔截器棧里面,存放了一些上面聲明好的攔截器                    * 攔截器棧相當于一個list集合,執(zhí)行的時候是按照存放的先后順序來執(zhí)行             -->            <interceptor-stack name="defaultStack">                <interceptor-ref name="exception"/>                <interceptor-ref name="alias"/>                <interceptor-ref name="servletConfig"/>                <interceptor-ref name="i18n"/>                <interceptor-ref name="prepare"/>                <interceptor-ref name="chain"/>                <interceptor-ref name="scopedModelDriven"/>                <interceptor-ref name="modelDriven"/>                <interceptor-ref name="fileUpload">                    <param name="maximumSize">20971520</param>                    <param name="allowedTypes">text/plain</param>                    <param name="allowedExtensions">.txt</param>                </interceptor-ref>                <interceptor-ref name="checkbox"/>                <interceptor-ref name="multiselect"/>                <interceptor-ref name="staticParams"/>                <interceptor-ref name="actionMappingParams"/>                <interceptor-ref name="params">                    <param name="excludeParams">dojo/..*,^struts/..*,^session/..*,^request/..*,^application/..*,^servlet(Request|Response)/..*,parameters/...*</param>                </interceptor-ref>                <interceptor-ref name="conversionError"/>                <interceptor-ref name="validation">                    <param name="excludeMethods">input,back,cancel,browse</param>                </interceptor-ref>                <interceptor-ref name="workflow">                    <param name="excludeMethods">input,back,cancel,browse</param>                </interceptor-ref>                <interceptor-ref name="debugging"/>                                <!-- 配置使用自定義攔截器 -->                <interceptor-ref name="expessionInterceptor"/>                            </interceptor-stack>       </interceptors>        <!-- 配置在struts2框架運行時,默認要執(zhí)行的是哪個攔截器棧,defaultStack -->        <default-interceptor-ref name="defaultStack"/>        <!-- 配置在struts2框架運行時,如果沒有為action指定class的話,默認要執(zhí)行的class的類名 -->        <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />    </package></struts>

3)struts-plugin.xml

如果不是開發(fā)插件的話,是不需要編寫這個配置文件的,一般是使用插件。

4)struts.xml

struts.xml 為Struts 2的核心配置文件,主要負責管理應(yīng)用中的Action映射,以及該Action包含的Result定義等。

  • 常量配置
  • 包配置
  • include包含配置
  • 攔截器配置
  • 全局result設(shè)置
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4     "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6     <!--  7         1、constant:配置常量 8             * name:指定的是struts2框架底層提供的default.properties資源文件中配置的"常量" 9             * value:指定的是配置常量的值10             * 在struts.xml文件中,配置的常量的值會覆蓋底層提供的default.properties資源文件中配置的常量的值11         12         * 配置struts2框架的頁面中請求連接的后綴名,如果指定多個的話,用","隔開13         * 如果在struts.xml中和struts.properties資源文件中同時進行配置,struts.properties的配置起作用14         * 因為常量可以在多個配置文件中進行定義,所以我們需要了解下struts2加載常量的搜索順序:15             1 struts-default.xml16             2 struts-plugin.xml17             3 struts.xml18             4 struts.properties(自己創(chuàng)建)19             5 web.xml20      -->21     <constant name="struts.devMode" value="true"></constant>22         23     <!-- 24         配置所有資源文件,省略后綴名,如果配置多個資源文件時,用","隔開。不僅是國際化資源文件25         * 類型轉(zhuǎn)換器的錯誤提示資源文件26         * 國際化資源文件27         * 上傳文件的錯誤提示信息資源文件28      -->29     <constant name="struts.custom.i18n.resources" 30             value="cn.sunny.converter.converter,31                     cn.sunny.i18n.resources,32                     cn.sunny.upload.fileuploadmessage">33     </constant>34     35     <!-- 配置文件上傳的總大小 -->36     <constant name="struts.multipart.maxSize" value="2097152000"></constant>37     38     39     <!--40         2、包配置41         package:包42           * name:包名,唯一的,必選項43           * namespace:命名空間,唯一的,相當于房間號??蛇x項,省略情況下是"/"。頁面中請求連接的前半部分44           * extends:繼承其他package45             * extends="struts-default":struts2框架底層提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件46     -->47     <package name="default" namespace="/" extends="struts-default">48         <!-- 49             action:50                 * name:對應(yīng)頁面中請求連接的后面半部分51                 * class:對應(yīng)要執(zhí)行的類的完整路徑52                 * method:要執(zhí)行的方法名稱,默認為execute方法53          -->54         <action name="testAction" class="com.sunny.action.TestAction" method="test">55             <!-- 56                 result:結(jié)果類型57                     * name:對應(yīng)的是執(zhí)行的類的方法的返回值58                     * 后半部分的文本內(nèi)容:要轉(zhuǎn)向到的頁面59              -->60             <result name="success">/success.jsp</result>61         </action>62     </package>63     64     <!-- 65         3、引入自定義配置文件 66         include節(jié)點是struts2中組件化的方式,67         可以將每個功能模塊獨立到一個xml配置文件中,68         然后用include節(jié)點引用 69     -->70     <include file="struts_user.xml"/>71 72     <!--4、攔截器配置,后面講攔截器的時候還會具體講解-->73     <interceptors>74         <!-- 定義攔截器 75             name:攔截器名稱76             class:攔截器類路徑77          -->78         <interceptor name="logger" class="com.sunny.logger"/>79         <!-- 定義攔截器棧 -->80         <interceptor-stack name="mystack">81             <interceptor-ref name="defaultStack"/>82             <interceptor-ref name="logger"/>83         </interceptor-stack>84     </interceptors>85     <!-- 配置修改struts2框架運行時,默認執(zhí)行的是自定義攔截器棧 -->86     <default-interceptor-ref name="expessionStack" />87 88     <!-- 5、全局results配置 -->89     <global-results>90         <result name="input">/error.jsp</result>91     </global-results>92 </struts>

5)struts.properties

struts.properties文件是一個標準的Properties文件,該文件包含了系列的key-value對象,每個key就是一個Struts 2屬性,該key對應(yīng)的value就是一個Struts 2屬性值。

Struts2在default.properties文件中給出了所有屬性的列表,并對其中一些屬性設(shè)置了默認值,如果想改變這些默認值或者給那些沒有在default.properties文件中設(shè)置值的屬性設(shè)置值,則可以使用struts.properties,如果設(shè)置了struts.properties文件,那么在該文件中的屬性會覆蓋default.properties文件中的屬性,struts.properties的使用方法和default.properties一樣。

struts.properties一般放在src目錄下。

6)web.xml

任何MVC框架都需要與Web應(yīng)用整合需要助于web.xml文件,只有配置在web.xml文件中Servlet才會被應(yīng)用加載。通常,所有的MVC框架都需要Web應(yīng)用加載一個核心控制器,對于Struts2框架而言,需要加載StrutsPrepareAndExecuteFilter,只要Web應(yīng)用負責加載StrutsPrepareAndExecuteFilter,StrutsPrepareAndExecuteFilter將會加載Struts2框架。因為Struts2將核心控制器設(shè)計成Filter,而不是一個普通Servlet。故為了讓W(xué)eb應(yīng)用加載StrutsPrepareAndExecuteFilter,只需要在web.xml文件中配置StrutsPrepareAndExecuteFilter即可。此外web.xml依然可以配置JavaEE信息,比如初始化信息,Servlet、Filter等。

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5  6     <display-name>Struts Blank</display-name> 7  8     <filter> 9         <filter-name>struts2</filter-name>10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>11     </filter>12 13     <filter-mapping>14         <filter-name>struts2</filter-name>15         <url-pattern>/*</url-pattern>16     </filter-mapping>17 18 </web-app>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 饶阳县| 佳木斯市| 井陉县| 禹州市| 泰来县| 高邑县| 孟津县| 黔西| 政和县| 偃师市| 崇明县| 安岳县| 萨嘎县| 拜城县| 茂名市| 长沙市| 天等县| 沂源县| 元谋县| 清水河县| 彰化市| 宜章县| 清河县| 兴山县| 都兰县| 文安县| 寿宁县| 进贤县| 凤冈县| 永修县| 垣曲县| 镇沅| 锡林浩特市| 麻栗坡县| 南平市| 洛川县| 吉安县| 慈利县| 湖北省| 平武县| 岚皋县|