星空网 > 软件开发 > Java

Struts2学习笔记(二)——配置详解

1、Struts2配置文件加载顺序:

  • default.properties(默认常量配置)
  • struts-default.
  • struts-plugin.
  • struts.
  • struts.properties(常量配置)
  • web.

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 //上传文件的最大字节数 6 struts.multipart.maxSize=2097152 7 //表单提交或者url请求时地址的后缀,如果需要指定多个请求后缀,则以英文逗号隔开 8 struts.action.extension=action,, 9 //Struts中action创建都是由对应的工厂创建。10 struts.objectFactory = spring11 //指定spring框架的自动装配类型,默认值为name,即默认根据bean的name属性自动装配12 struts.objectFactory.spring.autoWire = name13 //动态方法调用14 struts.enable.DynamicMethodInvocation = true15 //是否为开发模式16 struts.devMode = false17 //对于开发来讲还是必将重要的。设置为true时,在每次请求时,资源包就会被重载。18 struts.i18n.reload=false19 //设置为true时,我们每次修改struts.20 struts.configuration.false21 //页面使用静态方法。通过ognl标签调用值栈action中的方法。22 struts.ognl.allowStaticMethodAccess=false

default.properties是不能直接修改的,我们如果要修改,有两种方式:

  • 在src下创建struts.properties
    • 例如:struts.enable.DynamicMethodInvocation = true
  • 在struts.
  • 例如:<constant name="struts.devMode" value=”true” />

2)struts-default.

struts-default.

<??><!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:设置为抽象包,下面不能定义action标签   -->  <package name="struts-default" abstract="true">    <!--       result-types:声明结果类型        * name:结果类型的名称        * class:结果类型对应类的完整路径        * default:设置其为默认,true是默认     -->    <result-types>      <!-- 转发到action -->      <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>      <!-- 转发到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"/>      <!-- 重定向到jsp -->      <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:对应**类的完整路径     -->    <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集合,执行的时候是按照存放的先后顺序来执行       -->      <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框架运行时,默认要执行的是哪个**栈,defaultStack -->    <default-interceptor-ref name="defaultStack"/>    <!-- 配置在struts2框架运行时,如果没有为action指定class的话,默认要执行的class的类名 -->    <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />  </package></struts>

3)struts-plugin.

如果不是开发插件的话,是不需要编写这个配置文件的,一般是使用插件。

4)struts.

struts.

  • 常量配置
  • 包配置
  • include包含配置
  • **配置
  • 全局result设置
 1 <??> 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.11     12     * 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开13     * 如果在struts.14     * 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:15       1 struts-default.16       2 struts-plugin.17       3 struts.18       4 struts.properties(自己创建)19       5 web.20   -->21   <constant name="struts.devMode" value="true"></constant>22     23   <!-- 24     配置所有资源文件,省略后缀名,如果配置多个资源文件时,用","隔开。不仅是国际化资源文件25     * 类型转换器的错误提示资源文件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:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分44      * extends:继承其他package45       * extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.46   -->47   <package name="default" namespace="/" extends="struts-default">48     <!-- 49       action:50         * name:对应页面中请求连接的后面半部分51         * class:对应要执行的类的完整路径52         * method:要执行的方法名称,默认为execute方法53     -->54     <action name="testAction" class="com.sunny.action.TestAction" method="test">55       <!-- 56         result:结果类型57           * name:对应的是执行的类的方法的返回值58           * 后半部分的文本内容:要转向到的页面59       -->60       <result name="success">/success.jsp</result>61     </action>62   </package>63   64   <!-- 65     3、引入自定义配置文件 66     include节点是struts2中组件化的方式,67     可以将每个功能模块独立到一个68     然后用include节点引用 69   -->70   <include file="struts_user./>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框架运行时,默认执行的是自定义**栈 -->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对应的value就是一个Struts 2属性值。

Struts2在default.properties文件中给出了所有属性的列表,并对其中一些属性设置了默认值,如果想改变这些默认值或者给那些没有在default.properties文件中设置值的属性设置值,则可以使用struts.properties,如果设置了struts.properties文件,那么在该文件中的属性会覆盖default.properties文件中的属性,struts.properties的使用方法和default.properties一样。

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

6)web.

任何MVC框架都需要与Web应用整合需要助于web.

 1 <??> 2 <web-app id="WebApp_9" version="2.4" ="http://java.sun.com/ 3      ="http://www.w3.org/2001/ 4      xsi:schemaLocation="http://java.sun.com/> 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>




原标题:Struts2学习笔记(二)——配置详解

关键词:Struts

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Harry’s Razors Company:https://www.ikjzd.com/w/4314
empisports:https://www.ikjzd.com/w/4315
武汉有榜科技有限公司:https://www.ikjzd.com/w/4316
君誉物流:https://www.ikjzd.com/w/4317
世界知识产权组织制裁与调解中心:https://www.ikjzd.com/w/4318
世界知识产权组织调解规则(WIPO Mediation Rules):https://www.ikjzd.com/w/4319
十月北方旅游最佳去处?:https://www.vstour.cn/a/365184.html
缅甸电子签证口岸 缅甸电子签证在线申请:https://www.vstour.cn/a/365185.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流