Struts2配置文件詳解

一、struts-default默認配置文件 

Struts2框架中使用包來管理action,避免了Servlet在web.xml中難以管理的與維護的局面.包的作用和java中的類包是非常類似的,它主要用於管理一組業務功能相關的action,在實際應用中,我們應該把一組業務功能相關的action 放在同一個包下. 

配置包時必須指定name屬性,該name屬性值可以任意取名,但必須唯一,如果其他包要繼承該包,必須通過該屬性進行引用,包的namespace屬性用於定義該包的命名空間,命名空間作用爲訪問該包下的action路徑的一部分,見示例.namespace屬性可以不配置,如果不指定該屬性,默認的命名空間爲”” 

通常每個包都應該繼承struts-default包,因爲struts2很多核心功能都是攔截來實現的,如,從請求中把請求參數封閉到action,文件上傳和數據驗證等都是通過攔截器實現的,struts-default定義了這些攔截器和Result類型,可以這麼說,當包繼承了struts-default才能使用struts2提供的核心功能,struts-default包是在struts2-core-2.xx.jar文件中的struts-defalut.xml中定義,struts-default.xml也是struts2默認配置文件,struts2每次都會自動加載struts-default.xml文件. 

package還有一個abstract=”true”屬性,指定此包爲抽象包,和抽象類的概念差不多,說明此包只能被其他包繼承,則它裏面不允許包含action元素. 


下面貼上struts-default.xml文件的源代碼: 
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!--  
  3. /* 
  4.  * $Id: struts-default.xml 694960 2008-09-13 15:01:00Z rgielen $ 
  5.  * 
  6.  * Licensed to the Apache Software Foundation (ASF) under one 
  7.  * or more contributor license agreements.  See the NOTICE file 
  8.  * distributed with this work for additional information 
  9.  * regarding copyright ownership.  The ASF licenses this file 
  10.  * to you under the Apache License, Version 2.0 (the 
  11.  * "License"); you may not use this file except in compliance 
  12.  * with the License.  You may obtain a copy of the License at 
  13.  * 
  14.  *  http://www.apache.org/licenses/LICENSE-2.0 
  15.  * 
  16.  * Unless required by applicable law or agreed to in writing, 
  17.  * software distributed under the License is distributed on an 
  18.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
  19.  * KIND, either express or implied.  See the License for the 
  20.  * specific language governing permissions and limitations 
  21.  * under the License. 
  22.  */  
  23. -->  
  24.   
  25. <!DOCTYPE struts PUBLIC  
  26.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  27.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  28.   
  29. <struts>  
  30.     <bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />  
  31.     <bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" class="org.apache.struts2.impl.StrutsObjectFactory" />  
  32.   
  33.     <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="xwork" class="com.opensymphony.xwork2.DefaultActionProxyFactory"/>  
  34.     <bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class="org.apache.struts2.impl.StrutsActionProxyFactory"/>  
  35.   
  36.     <bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="tiger" class="com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer"/>  
  37.     <bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="notiger" class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>  
  38.     <bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="struts" class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>  
  39.   
  40.     <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" class="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />  
  41.     <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="composite" class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" />  
  42.     <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="restful" class="org.apache.struts2.dispatcher.mapper.RestfulActionMapper" />  
  43.     <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="restful2" class="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />  
  44.   
  45.     <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="struts" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" optional="true"/>  
  46.     <bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="jakarta" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" optional="true" />  
  47.   
  48.     <bean type="org.apache.struts2.views.TagLibrary" name="s" class="org.apache.struts2.views.DefaultTagLibrary" />  
  49.   
  50.     <bean class="org.apache.struts2.views.freemarker.FreemarkerManager" name="struts" optional="true"/>  
  51.     <bean class="org.apache.struts2.views.velocity.VelocityManager" name="struts" optional="true" />  
  52.   
  53.     <bean class="org.apache.struts2.components.template.TemplateEngineManager" />  
  54.     <bean type="org.apache.struts2.components.template.TemplateEngine" name="ftl" class="org.apache.struts2.components.template.FreemarkerTemplateEngine" />  
  55.     <bean type="org.apache.struts2.components.template.TemplateEngine" name="vm" class="org.apache.struts2.components.template.VelocityTemplateEngine" />  
  56.     <bean type="org.apache.struts2.components.template.TemplateEngine" name="jsp" class="org.apache.struts2.components.template.JspTemplateEngine" />  
  57.   
  58.     <bean type="com.opensymphony.xwork2.util.XWorkConverter" name="xwork1" class="com.opensymphony.xwork2.util.XWorkConverter" />  
  59.     <bean type="com.opensymphony.xwork2.util.XWorkConverter" name="struts" class="com.opensymphony.xwork2.util.AnnotationXWorkConverter" />  
  60.     <bean type="com.opensymphony.xwork2.TextProvider" name="xwork1" class="com.opensymphony.xwork2.TextProviderSupport" />  
  61.     <bean type="com.opensymphony.xwork2.TextProvider" name="struts" class="com.opensymphony.xwork2.TextProviderSupport" />  
  62.   
  63.     <!--  Only have static injections -->  
  64.     <bean class="com.opensymphony.xwork2.ObjectFactory" static="true" />  
  65.     <bean class="com.opensymphony.xwork2.util.XWorkConverter" static="true" />  
  66.     <bean class="com.opensymphony.xwork2.util.OgnlValueStack" static="true" />  
  67.     <bean class="com.opensymphony.xwork2.validator.ValidatorFileParser" static="true" />  
  68.     <bean class="org.apache.struts2.dispatcher.Dispatcher" static="true" />  
  69.     <bean class="org.apache.struts2.components.Include" static="true" />  
  70.     <bean class="org.apache.struts2.dispatcher.FilterDispatcher" static="true" />  
  71.     <bean class="org.apache.struts2.views.util.ContextUtil" static="true" />  
  72.     <bean class="org.apache.struts2.views.util.UrlHelper" static="true" />  
  73.   
  74.     <package name="struts-default" abstract="true">  
  75.         <result-types>  
  76.             <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>  
  77.             <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>  
  78.             <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>  
  79.             <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>  
  80.             <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>  
  81.             <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
  82.             <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>  
  83.             <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>  
  84.             <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>  
  85.             <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />  
  86.             <!-- Deprecated name form scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->  
  87.             <result-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>  
  88.             <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />  
  89.         </result-types>  
  90.   
  91.   
  92.         <interceptors>  
  93.             <interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>  
  94.             <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>  
  95.             <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>  
  96.             <interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>  
  97.             <interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>  
  98.             <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />  
  99.             <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />  
  100.             <interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>  
  101.             <interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>  
  102.             <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>  
  103.             <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>  
  104.             <interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>  
  105.             <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>  
  106.             <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>  
  107.             <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>  
  108.             <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>  
  109.             <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>  
  110.             <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>  
  111.             <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>  
  112.             <interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>  
  113.             <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>  
  114.             <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>  
  115.             <interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>  
  116.             <interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>  
  117.             <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>  
  118.             <interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>  
  119.             <interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />  
  120.             <interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />  
  121.             <interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />  
  122.             <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />  
  123.   
  124.             <!-- Basic stack -->  
  125.             <interceptor-stack name="basicStack">  
  126.                 <interceptor-ref name="exception"/>  
  127.                 <interceptor-ref name="servletConfig"/>  
  128.                 <interceptor-ref name="prepare"/>  
  129.                 <interceptor-ref name="checkbox"/>  
  130.                 <interceptor-ref name="params"/>  
  131.                 <interceptor-ref name="conversionError"/>  
  132.             </interceptor-stack>  
  133.   
  134.             <!-- Sample validation and workflow stack -->  
  135.             <interceptor-stack name="validationWorkflowStack">  
  136.                 <interceptor-ref name="basicStack"/>  
  137.                 <interceptor-ref name="validation"/>  
  138.                 <interceptor-ref name="workflow"/>  
  139.             </interceptor-stack>  
  140.   
  141.             <!-- Sample file upload stack -->  
  142.             <interceptor-stack name="fileUploadStack">  
  143.                 <interceptor-ref name="fileUpload"/>  
  144.                 <interceptor-ref name="basicStack"/>  
  145.             </interceptor-stack>  
  146.   
  147.             <!-- Sample model-driven stack  -->  
  148.             <interceptor-stack name="modelDrivenStack">  
  149.                 <interceptor-ref name="modelDriven"/>  
  150.                 <interceptor-ref name="basicStack"/>  
  151.             </interceptor-stack>  
  152.   
  153.             <!-- Sample action chaining stack -->  
  154.             <interceptor-stack name="chainStack">  
  155.                 <interceptor-ref name="chain"/>  
  156.                 <interceptor-ref name="basicStack"/>  
  157.             </interceptor-stack>  
  158.   
  159.             <!-- Sample i18n stack -->  
  160.             <interceptor-stack name="i18nStack">  
  161.                 <interceptor-ref name="i18n"/>  
  162.                 <interceptor-ref name="basicStack"/>  
  163.             </interceptor-stack>  
  164.   
  165.             <!-- An example of the params-prepare-params trick. This stack  
  166.                  is exactly the same as the defaultStack, except that it  
  167.                  includes one extra interceptor before the prepare interceptor:  
  168.                  the params interceptor.  
  169.   
  170.                  This is useful for when you wish to apply parameters directly  
  171.                  to an object that you wish to load externally (such as a DAO  
  172.                  or database or service layer), but can't load that object  
  173.                  until at least the ID parameter has been loaded. By loading  
  174.                  the parameters twice, you can retrieve the object in the  
  175.                  prepare() method, allowing the second params interceptor to  
  176.                  apply the values on the object. -->  
  177.             <interceptor-stack name="paramsPrepareParamsStack">  
  178.                 <interceptor-ref name="exception"/>  
  179.                 <interceptor-ref name="alias"/>  
  180.                 <interceptor-ref name="params"/>  
  181.                 <interceptor-ref name="servletConfig"/>  
  182.                 <interceptor-ref name="prepare"/>  
  183.                 <interceptor-ref name="i18n"/>  
  184.                 <interceptor-ref name="chain"/>  
  185.                 <interceptor-ref name="modelDriven"/>  
  186.                 <interceptor-ref name="fileUpload"/>  
  187.                 <interceptor-ref name="checkbox"/>  
  188.                 <interceptor-ref name="staticParams"/>  
  189.                 <interceptor-ref name="params"/>  
  190.                 <interceptor-ref name="conversionError"/>  
  191.                 <interceptor-ref name="validation">  
  192.                     <param name="excludeMethods">input,back,cancel</param>  
  193.                 </interceptor-ref>  
  194.                 <interceptor-ref name="workflow">  
  195.                     <param name="excludeMethods">input,back,cancel</param>  
  196.                 </interceptor-ref>  
  197.             </interceptor-stack>  
  198.   
  199.             <!-- A complete stack with all the common interceptors in place.  
  200.                  Generally, this stack should be the one you use, though it  
  201.                  may do more than you need. Also, the ordering can be  
  202.                  switched around (ex: if you wish to have your servlet-related  
  203.                  objects applied before prepare() is called, you'd need to move  
  204.                  servlet-config interceptor up.  
  205.   
  206.                  This stack also excludes from the normal validation and workflow  
  207.                  the method names input, back, and cancel. These typically are  
  208.                  associated with requests that should not be validated.  
  209.                  -->  
  210.             <interceptor-stack name="defaultStack">  
  211.                 <interceptor-ref name="exception"/>  
  212.                 <interceptor-ref name="alias"/>  
  213.                 <interceptor-ref name="servletConfig"/>  
  214.                 <interceptor-ref name="prepare"/>  
  215.                 <interceptor-ref name="i18n"/>  
  216.                 <interceptor-ref name="chain"/>  
  217.                 <interceptor-ref name="debugging"/>  
  218.                 <interceptor-ref name="profiling"/>  
  219.                 <interceptor-ref name="scopedModelDriven"/>  
  220.                 <interceptor-ref name="modelDriven"/>  
  221.                 <interceptor-ref name="fileUpload"/>  
  222.                 <interceptor-ref name="checkbox"/>  
  223.                 <interceptor-ref name="staticParams"/>  
  224.                 <interceptor-ref name="params">  
  225.                   <param name="excludeParams">dojo\..*</param>  
  226.                 </interceptor-ref>  
  227.                 <interceptor-ref name="conversionError"/>  
  228.                 <interceptor-ref name="validation">  
  229.                     <param name="excludeMethods">input,back,cancel,browse</param>  
  230.                 </interceptor-ref>  
  231.                 <interceptor-ref name="workflow">  
  232.                     <param name="excludeMethods">input,back,cancel,browse</param>  
  233.                 </interceptor-ref>  
  234.             </interceptor-stack>  
  235.   
  236.             <!-- The completeStack is here for backwards compatibility for  
  237.                  applications that still refer to the defaultStack by the  
  238.                  old name -->  
  239.             <interceptor-stack name="completeStack">  
  240.                 <interceptor-ref name="defaultStack"/>  
  241.             </interceptor-stack>  
  242.   
  243.             <!-- Sample execute and wait stack.  
  244.                  Note: execAndWait should always be the *last* interceptor. -->  
  245.             <interceptor-stack name="executeAndWaitStack">  
  246.                 <interceptor-ref name="execAndWait">  
  247.                     <param name="excludeMethods">input,back,cancel</param>  
  248.                 </interceptor-ref>  
  249.                 <interceptor-ref name="defaultStack"/>  
  250.                 <interceptor-ref name="execAndWait">  
  251.                     <param name="excludeMethods">input,back,cancel</param>  
  252.                 </interceptor-ref>  
  253.             </interceptor-stack>  
  254.   
  255.             <!-- Deprecated name forms scheduled for removal in Struts 2.1.0. The camelCase versions are preferred. See ww-1707 -->  
  256.             <interceptor name="external-ref" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>  
  257.             <interceptor name="model-driven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>  
  258.             <interceptor name="static-params" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>  
  259.             <interceptor name="scoped-model-driven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>  
  260.             <interceptor name="servlet-config" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>  
  261.             <interceptor name="token-session" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>  
  262.   
  263.        </interceptors>  
  264.   
  265.         <default-interceptor-ref name="defaultStack"/>  
  266.     </package>  
  267.   
  268. </struts>  


二、struts2配置文件中常用常量配置列表: 
<constant name="struts.i18n.encoding" value="UTF-8" /> 
指定Web應用的默認編碼集,相當於調用HttpServletRequest的setCharacterEncoding方法 

<constant name="struts.action.extension" value="do" /> 
該屬性指定需要Struts 2處理的請求後綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts 2處理。    如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。   

<constant name="struts.serve.static.browserCache " value="false" /> 
設置瀏覽器是否緩存靜態內容,默認值爲true,開發階段最好false 

<constant name="struts.configuration.xml.reload" value="true" /> 
當struts的配置文件修改後,系統是否自動重新加載該文件,默認值爲false,開發階段最好true 

<constant name="struts.devMode" value="true" /> 
開發模式下設爲true,這樣可以打印出更詳細的錯誤信息 

<constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
動態方法調用,可以解決多個請求對應一個Servlet的問題,後面詳細講解,默認爲true,關閉則設爲false. 

這裏只是列舉了一些常用的開關,當然還有許多其他的開關,後面的學習中會逐漸介紹,大家在這裏先了解一下. 

以下是從網上摘得的,比較全的一個資料 
struts.serve.static.browserCache 該屬性設置瀏覽器是否緩存靜態內容。當應用處於開發階段時,我們希望每次請求都獲得服務器的最新響應,則可設置該屬性爲false。 

struts.enable.DynamicMethodInvocation 該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。如果需要關閉動態方法調用,則可設置該屬性爲false。 

struts.enable.SlashesInActionNames 該屬性設置Struts 2是否允許在Action名中使用斜線,該屬性的默認值是false。如果開發者希望允許在Action名中使用斜線,則可設置該屬性爲true。 

struts.tag.altSyntax 該屬性指定是否允許在Struts 2標籤中使用表達式語法,因爲通常都需要在標籤中使用表達式語法,故此屬性應該設置爲true,該屬性的默認值是true。 

struts.devMode該屬性設置Struts 2應用是否使用開發模式。如果設置該屬性爲true,則可以在應用出錯時顯示更多、更友好的出錯提示。該屬性只接受true和flase兩個值,該屬性的默認值是false。通常,應用在開發階段,將該屬性設置爲true,當進入產品發佈階段後,則該屬性設置爲false。 

struts.i18n.reload該屬性設置是否每次HTTP請求到達時,系統都重新加載資源文件。該屬性默認值是false。在開發階段將該屬性設置爲true會更有利於開發,但在產品發佈階段應將該屬性設置爲false。 

提示 開發階段將該屬性設置了true,將可以在每次請求時都重新加載國際化資源文件,從而可以讓開發者看到實時開發效果;產品發佈階段應該將該屬性設置爲false,是爲了提供響應性能,每次請求都需要重新加載資源文件會大大降低應用的性能。 

struts.ui.theme該屬性指定視圖標籤默認的視圖主題,該屬性的默認值是xhtml。 

struts.ui.templateDir該屬性指定視圖主題所需要模板文件的位置,該屬性的默認值是template,即默認加載template路徑下的模板文件。 

struts.ui.templateSuffix該屬性指定模板文件的後綴,該屬性的默認屬性值是ftl。該屬性還允許使用ftl、vm或jsp,分別對應FreeMarker、Velocity和JSP模板。 

struts.configuration.xml.reload該屬性設置當struts.xml文件改變後,系統是否自動重新加載該文件。該屬性的默認值是false。 

struts.velocity.configfile該屬性指定Velocity框架所需的velocity.properties文件的位置。該屬性的默認值爲velocity.properties。 

struts.velocity.contexts該屬性指定Velocity框架的Context位置,如果該框架有多個Context,則多個Context之間以英文逗號(,)隔開。 

struts.velocity.toolboxlocation該屬性指定Velocity框架的toolbox的位置。 

struts.url.http.port該屬性指定Web應用所在的監聽端口。該屬性通常沒有太大的用戶,只是當Struts 2需要生成URL時(例如Url標籤),該屬性才提供Web應用的默認端口。 

struts.url.https.port該屬性類似於struts.url.http.port屬性的作用,區別是該屬性指定的是Web應用的加密服務端口。 

struts.url.includeParams該屬性指定Struts 2生成URL時是否包含請求參數。該屬性接受none、get和all三個屬性值,分別對應於不包含、僅包含GET類型請求參數和包含全部請求參數。 


struts.custom.i18n.resources該屬性指定Struts 2應用所需要的國際化資源文件,如果有多份國際化資源文件,則多個資源文件的文件名以英文逗號(,)隔開。 


struts.dispatcher.parametersWorkaround 對於某些Java EE服務器,不支持HttpServlet Request調用getParameterMap()方法,此時可以設置該屬性值爲true來解決該問題。該屬性的默認值是false。對於WebLogic、Orion和OC4J服務器,通常應該設置該屬性爲true。 

struts.freemarker.manager.classname 該屬性指定Struts 2使用的FreeMarker管理器。該屬性的默認值是org.apache.struts2.views.freemarker.FreemarkerManager,這是Struts 2內建的FreeMarker管理器。 

struts.freemarker.wrapper.altMap該屬性只支持true和false兩個屬性值,默認值是true。通常無需修改該屬性值。 

struts.xslt.nocache 該屬性指定XSLT Result是否使用樣式表緩存。當應用處於開發階段時,該屬性通常被設置爲true;當應用處於產品使用階段時,該屬性通常被設置爲false。 

struts.configuration.files 該屬性指定Struts 2框架默認加載的配置文件,如果需要指定默認加載多個配置文件,則多個配置文件的文件名之間以英文逗號(,)隔開。該屬性的默認值爲struts-default.xml,struts-plugin.xml,struts.xml,看到該屬性值,讀者應該明白爲什麼Struts 2框架默認加載struts.xml文件了。 

三、struts2框架中的常識介紹 
1、在請求時,路徑後的後綴action可要可不要,即下面的兩種請求都是可以的 
http://localhost:8080/Struts2/chapter1/HelloWorld 
http://localhost:8080/Struts2/chapter1/HelloWorld.action 
2、 
<action name="Login"> 
  <result>/WEB-INF/JspPage/chapter1/Login.jsp</result> 
</action> 
我們發現,當我們請求的路徑爲http://localhost:8080/Struts2/chapter1/Login時,同樣可以實現頁面的跳轉,這是怎麼回事呢? 
如果沒有爲action指定class,默認是ActionSupport類<action name="Login"> 
相當於 
<action name="Login" class="com.opensymphony.xwork2.ActionSupport"> 
如果沒有爲action指定method,默認執行action中的execute()方法 
<action name="Login">相當於 
<action name="Login" class="com.opensymphony.xwork2.ActionSupport" 
method="execute"> 
如果沒有指定result的name屬性,默認值爲success. 
<result>相當於<result name="success"> 

四、ActionSupport這個類到底是個什麼類,爲什麼自己寫的action都要默認繼承它? 
struts2不要求我們自己設計的action類繼承任何的struts基類或struts接口,但是我們爲了方便實現我們自己的action,大多數情況下都會繼承com.opensymphony.xwork2.ActionSupport類,並重寫此類裏的public String execute() throws Exception方法。因爲此類中實現了很多的實用藉口,提供了很多默認方法,這些默認方法包括國際化信息的方法、默認的處理用戶請求的方法等,這樣可以大大的簡化Acion的開發。 

下面貼上ActionSupport.action的源代碼 
Java代碼  收藏代碼
  1. // Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.  
  2. // Jad home page: http://kpdus.tripod.com/jad.html  
  3. // Decompiler options: packimports(3) fieldsfirst ansi space   
  4. // Source File Name:   ActionSupport.java  
  5.   
  6. package com.opensymphony.xwork2;  
  7.   
  8. import com.opensymphony.xwork2.util.ValueStack;  
  9. import java.io.Serializable;  
  10. import java.util.*;  
  11. import org.apache.commons.logging.Log;  
  12. import org.apache.commons.logging.LogFactory;  
  13.   
  14. // Referenced classes of package com.opensymphony.xwork2:  
  15. //          TextProviderFactory, ValidationAwareSupport, Action, Validateable,   
  16. //          ValidationAware, TextProvider, LocaleProvider, ActionContext  
  17.   
  18. public class ActionSupport  
  19.     implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable  
  20. {  
  21.   
  22.     protected static Log LOG = LogFactory.getLog(com/opensymphony/xwork2/ActionSupport);  
  23.     private final transient TextProvider textProvider = (new TextProviderFactory()).createInstance(getClass(), this);  
  24.     private final ValidationAwareSupport validationAware = new ValidationAwareSupport();  
  25.   
  26.     public ActionSupport()  
  27.     {  
  28.     }  
  29.   
  30.     public void setActionErrors(Collection errorMessages)  
  31.     {  
  32.         validationAware.setActionErrors(errorMessages);  
  33.     }  
  34.   
  35.     public Collection getActionErrors()  
  36.     {  
  37.         return validationAware.getActionErrors();  
  38.     }  
  39.   
  40.     public void setActionMessages(Collection messages)  
  41.     {  
  42.         validationAware.setActionMessages(messages);  
  43.     }  
  44.   
  45.     public Collection getActionMessages()  
  46.     {  
  47.         return validationAware.getActionMessages();  
  48.     }  
  49.   
  50.     /** 
  51.      * @deprecated Method getErrorMessages is deprecated 
  52.      */  
  53.   
  54.     public Collection getErrorMessages()  
  55.     {  
  56.         return getActionErrors();  
  57.     }  
  58.   
  59.     /** 
  60.      * @deprecated Method getErrors is deprecated 
  61.      */  
  62.   
  63.     public Map getErrors()  
  64.     {  
  65.         return getFieldErrors();  
  66.     }  
  67.   
  68.     public void setFieldErrors(Map errorMap)  
  69.     {  
  70.         validationAware.setFieldErrors(errorMap);  
  71.     }  
  72.   
  73.     public Map getFieldErrors()  
  74.     {  
  75.         return validationAware.getFieldErrors();  
  76.     }  
  77.   
  78.     public Locale getLocale()  
  79.     {  
  80.         return ActionContext.getContext().getLocale();  
  81.     }  
  82.   
  83.     public String getText(String aTextName)  
  84.     {  
  85.         return textProvider.getText(aTextName);  
  86.     }  
  87.   
  88.     public String getText(String aTextName, String defaultValue)  
  89.     {  
  90.         return textProvider.getText(aTextName, defaultValue);  
  91.     }  
  92.   
  93.     public String getText(String aTextName, String defaultValue, String obj)  
  94.     {  
  95.         return textProvider.getText(aTextName, defaultValue, obj);  
  96.     }  
  97.   
  98.     public String getText(String aTextName, List args)  
  99.     {  
  100.         return textProvider.getText(aTextName, args);  
  101.     }  
  102.   
  103.     public String getText(String key, String args[])  
  104.     {  
  105.         return textProvider.getText(key, args);  
  106.     }  
  107.   
  108.     public String getText(String aTextName, String defaultValue, List args)  
  109.     {  
  110.         return textProvider.getText(aTextName, defaultValue, args);  
  111.     }  
  112.   
  113.     public String getText(String key, String defaultValue, String args[])  
  114.     {  
  115.         return textProvider.getText(key, defaultValue, args);  
  116.     }  
  117.   
  118.     public String getText(String key, String defaultValue, List args, ValueStack stack)  
  119.     {  
  120.         return textProvider.getText(key, defaultValue, args, stack);  
  121.     }  
  122.   
  123.     public String getText(String key, String defaultValue, String args[], ValueStack stack)  
  124.     {  
  125.         return textProvider.getText(key, defaultValue, args, stack);  
  126.     }  
  127.   
  128.     public ResourceBundle getTexts()  
  129.     {  
  130.         return textProvider.getTexts();  
  131.     }  
  132.   
  133.     public ResourceBundle getTexts(String aBundleName)  
  134.     {  
  135.         return textProvider.getTexts(aBundleName);  
  136.     }  
  137.   
  138.     public void addActionError(String anErrorMessage)  
  139.     {  
  140.         validationAware.addActionError(anErrorMessage);  
  141.     }  
  142.   
  143.     public void addActionMessage(String aMessage)  
  144.     {  
  145.         validationAware.addActionMessage(aMessage);  
  146.     }  
  147.   
  148.     public void addFieldError(String fieldName, String errorMessage)  
  149.     {  
  150.         validationAware.addFieldError(fieldName, errorMessage);  
  151.     }  
  152.   
  153.     public String input()  
  154.         throws Exception  
  155.     {  
  156.         return "input";  
  157.     }  
  158.   
  159.     public String doDefault()  
  160.         throws Exception  
  161.     {  
  162.         return "success";  
  163.     }  
  164.   
  165.     public String execute()  
  166.         throws Exception  
  167.     {  
  168.         return "success";  
  169.     }  
  170.   
  171.     public boolean hasActionErrors()  
  172.     {  
  173.         return validationAware.hasActionErrors();  
  174.     }  
  175.   
  176.     public boolean hasActionMessages()  
  177.     {  
  178.         return validationAware.hasActionMessages();  
  179.     }  
  180.   
  181.     public boolean hasErrors()  
  182.     {  
  183.         return validationAware.hasErrors();  
  184.     }  
  185.   
  186.     public boolean hasFieldErrors()  
  187.     {  
  188.         return validationAware.hasFieldErrors();  
  189.     }  
  190.   
  191.     public void clearErrorsAndMessages()  
  192.     {  
  193.         validationAware.clearErrorsAndMessages();  
  194.     }  
  195.   
  196.     public void validate()  
  197.     {  
  198.     }  
  199.   
  200.     public Object clone()  
  201.         throws CloneNotSupportedException  
  202.     {  
  203.         return super.clone();  
  204.     }  
  205.   
  206.     public void pause(String s)  
  207.     {  
  208.     }  
  209.   
  210. }  

發佈了2 篇原創文章 · 獲贊 21 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章