ITeye博客

Struts2配置文件、拦截器等详解(转)

ericxu131 · 2008-03-24 15:46 · java & ee · Markdown

Struts2中有关struts-default.xml,struts.xml,struts.properties文件详解(转自)

每学习一个框架,我们都免不了要学习一些关于的配置文件,struts2也不例外,下面我就讲一下struts2中几个主要的配置文件。

1) struts-default.xml

这个文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器。

<bean class="com.opensymphony.xwork2.ObjectFactory" name="xwork" />

<bean type="com.opensymphony.xwork2.ObjectFactory" name="struts" class="org.apache.struts2.impl.StrutsObjectFactory" />



<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="xwork" class="com.opensymphony.xwork2.DefaultActionProxyFactory"/>

<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="struts" class="org.apache.struts2.impl.StrutsActionProxyFactory"/>
<bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="tiger" class="com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer"/>

<bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="notiger" class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>

<bean type="com.opensymphony.xwork2.util.ObjectTypeDeterminer" name="struts" class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>
<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="struts" class="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="composite" class="org.apache.struts2.dispatcher.mapper.CompositeActionMapper" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="restful" class="org.apache.struts2.dispatcher.mapper.RestfulActionMapper" />

<bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="restful2" class="org.apache.struts2.dispatcher.mapper.Restful2ActionMapper" />



<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="struts" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" optional="true"/>

<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="jakarta" class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest" scope="default" optional="true" />
<bean type="org.apache.struts2.views.TagLibrary" name="s" class="org.apache.struts2.views.DefaultTagLibrary" />
<bean class="org.apache.struts2.views.freemarker.FreemarkerManager" name="struts" optional="true"/>

<bean class="org.apache.struts2.views.velocity.VelocityManager" name="struts" optional="true" />



<bean class="org.apache.struts2.components.template.TemplateEngineManager" />

<bean type="org.apache.struts2.components.template.TemplateEngine" name="ftl" class="org.apache.struts2.components.template.FreemarkerTemplateEngine" />

<bean type="org.apache.struts2.components.template.TemplateEngine" name="vm" class="org.apache.struts2.components.template.VelocityTemplateEngine" />

<bean type="org.apache.struts2.components.template.TemplateEngine" name="jsp" class="org.apache.struts2.components.template.JspTemplateEngine" />
<bean type="com.opensymphony.xwork2.util.XWorkConverter" name="xwork1" class="com.opensymphony.xwork2.util.XWorkConverter" />

<bean type="com.opensymphony.xwork2.util.XWorkConverter" name="struts" class="com.opensymphony.xwork2.util.AnnotationXWorkConverter" />

<bean type="com.opensymphony.xwork2.TextProvider" name="xwork1" class="com.opensymphony.xwork2.TextProviderSupport" />

<bean type="com.opensymphony.xwork2.TextProvider" name="struts" class="com.opensymphony.xwork2.TextProviderSupport" />



<!--  Struts2中一些可以静态注入的bean,也就是不需要实例化的 -->

<bean class="com.opensymphony.xwork2.ObjectFactory" static="true" />

<bean class="com.opensymphony.xwork2.util.XWorkConverter" static="true" />

<bean class="com.opensymphony.xwork2.util.OgnlValueStack" static="true" />

<bean class="org.apache.struts2.dispatcher.Dispatcher" static="true" />

<bean class="org.apache.struts2.components.Include" static="true" />

<bean class="org.apache.struts2.dispatcher.FilterDispatcher" static="true" />

<bean class="org.apache.struts2.views.util.ContextUtil" static="true" />

<bean class="org.apache.struts2.views.util.UrlHelper" static="true" />
<package name="struts-default" abstract="true">

<!-- 结果类型的种类-->

    <result-types>

        <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>

        <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"/>

        <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>

        <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-type name="redirect-action" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

        <result-type name="plaintext" class="org.apache.struts2.dispatcher.PlainTextResult" />

    </result-types>
    <interceptors>

    <!--实现在不同请求中相似参数别名的准换-->

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

        <!--与Spring整合时自动装配的拦截器-->

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

        <!--构建一个action链,使当前action可以访问前一个action,与<result-type="chain" />配合使用-->

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

        <!--负责类型转换的拦截器-->

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

        <!--使用配置的name,value来是指cookies -->

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

       <!--负责创建httpSession-->

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

        <!--输出调试信息-->

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

        <!--扩展引用-->

        <interceptor name="externalRef" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

        <!--后台执行action负责发送等待画面给用户-->

        <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"/>

        <!--模型拦截器,当action实现了ModelDriven接口时,负责把getModel的结果放入valueStack中-->

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

        <!--有生命周期的ModelDriven-->

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

        <!--负责解析请求中的参数,并赋值给action中对应的属性-->

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

        <!--实现该Preparable接口的action,会调用拦截器的prepare方法-->

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

        <!--负责将action 标签下的param参数值传递给action实例-->

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

        <!--范围转换-->

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

        <!--用于访问Servlet API-->

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

        

        <interceptor name="sessionAutowiring" class="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor"/>

        <!--输出action执行时间-->

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

        <!--防止表单重复提交-->

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

        <!--与token拦截器相似,只是把token保存到HttpSession-->

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

        <!--负责表单字段的验证 *-validation.xml-->

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

        <!--负责执行action的validate()-->

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

        <!--存储和重新获取Action 消息/错误/字段错误为Action,实现ValidationAware接口到seesion-->

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

        <!--添加自动checkbox处理代码,这样检探测checkbox和添加它作为一个参数使用默认值(通常’false’).使用一个指定名字隐藏字段探测没提交的checkbox-->

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

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

        <!--JAAS服务拦截器-->

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



        <!-- 一个基本的拦截器栈 -->

        <interceptor-stack name="basicStack">

            <interceptor-ref name="exception"/>

            <interceptor-ref name="servletConfig"/>

            <interceptor-ref name="prepare"/>

            <interceptor-ref name="checkbox"/>

            <interceptor-ref name="params"/>

            <interceptor-ref name="conversionError"/>

        </interceptor-stack>



        <!-- 简单的validtion和webflow栈 -->

        <interceptor-stack name="validationWorkflowStack">

            <interceptor-ref name="basicStack"/>

            <interceptor-ref name="validation"/>

            <interceptor-ref name="workflow"/>

        </interceptor-stack>



        <!-- 文件上传的拦截器栈 -->

        <interceptor-stack name="fileUploadStack">

            <interceptor-ref name="fileUpload"/>

            <interceptor-ref name="basicStack"/>

        </interceptor-stack>



        <!-- model-driven 栈  -->

        <interceptor-stack name="modelDrivenStack">

            <interceptor-ref name="modelDriven"/>

            <interceptor-ref name="basicStack"/>

        </interceptor-stack>



        <!-- action链的拦截器栈 -->

        <interceptor-stack name="chainStack">

            <interceptor-ref name="chain"/>

            <interceptor-ref name="basicStack"/>

        </interceptor-stack>



        <!--  i18n 拦截器栈 -->

        <interceptor-stack name="i18nStack">

            <interceptor-ref name="i18n"/>

            <interceptor-ref name="basicStack"/>

        </interceptor-stack>



        <!-- 结合preparable和ModenDriven拦截器-->

        <interceptor-stack name="paramsPrepareParamsStack">

            <interceptor-ref name="exception"/>

            <interceptor-ref name="alias"/>

            <interceptor-ref name="params"/>

            <interceptor-ref name="servletConfig"/>

            <interceptor-ref name="prepare"/>

            <interceptor-ref name="i18n"/>

            <interceptor-ref name="chain"/>

            <interceptor-ref name="modelDriven"/>

            <interceptor-ref name="fileUpload"/>

            <interceptor-ref name="checkbox"/>

            <interceptor-ref name="staticParams"/>

            <interceptor-ref name="params"/>

            <interceptor-ref name="conversionError"/>

            <interceptor-ref name="validation">

                <param name="excludeMethods">input,back,cancel</param>

            </interceptor-ref>

            <interceptor-ref name="workflow">

                <param name="excludeMethods">input,back,cancel</param>

            </interceptor-ref>

        </interceptor-stack>



        <!--定义默认的拦截器栈  -->

        <interceptor-stack name="defaultStack">

            <interceptor-ref name="exception"/>

            <interceptor-ref name="alias"/>

            <interceptor-ref name="servletConfig"/>

            <interceptor-ref name="prepare"/>

            <interceptor-ref name="i18n"/>

            <interceptor-ref name="chain"/>

            <interceptor-ref name="debugging"/>

            <interceptor-ref name="profiling"/>

            <interceptor-ref name="scopedModelDriven"/>

            <interceptor-ref name="modelDriven"/>

            <interceptor-ref name="fileUpload"/>

            <interceptor-ref name="checkbox"/>

            <interceptor-ref name="staticParams"/>

            <interceptor-ref name="params">

              <param name="excludeParams">dojo\..*</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-stack>



        <interceptor-stack name="completeStack">

            <interceptor-ref name="defaultStack"/>

        </interceptor-stack>



        

        <interceptor-stack name="executeAndWaitStack">

            <interceptor-ref name="execAndWait">

                <param name="excludeMethods">input,back,cancel</param>

            </interceptor-ref>

            <interceptor-ref name="defaultStack"/>

            <interceptor-ref name="execAndWait">

                <param name="excludeMethods">input,back,cancel</param>

            </interceptor-ref>

        </interceptor-stack>



        

        <interceptor name="external-ref" class="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor"/>

        <interceptor name="model-driven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>

        <interceptor name="static-params" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>

        <interceptor name="scoped-model-driven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>

        <interceptor name="servlet-config" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

        <interceptor name="token-session" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>



   </interceptors>
    <default-interceptor-ref name="defaultStack"/>

</package>

2) struts.xml

该文件也是struts2框架自动加载的文件,在这个文件中可以定义一些自己的action,interceptor,package等,该文件的package 通常继承struts-default包。下面是这个文件的格式。

<package name="必填的包名" extends="" namespace="" abstract=""

externalReferenceResolver>

?

映射资源

异常处理资源

映射资源

异常处理资源

*

3) struts.properties文件

这个文件是struts2框架的全局属性文件,也是自动加载的文件。该文件包含了系列的key-value对。该文件完全可以配置在struts.xml文件中,使用constant元素。下面是这个文件中一些常见的配置项及说明。

指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration

开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。

struts.configuration=org.apache.struts2.config.DefaultConfiguration

设置默认的locale和字符编码

struts.locale=en_US

struts.i18n.encoding=UTF-8

指定struts的工厂类

struts.objectFactory = spring

指定spring框架的装配模式

装配方式有: name, type, auto, and constructor (name 是默认装配模式)

struts.objectFactory.spring.autoWire = name

该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true.

struts.objectFactory.spring.useClassCache = true

指定类型检查

#struts.objectTypeDeterminer = tiger

#struts.objectTypeDeterminer = notiger

该属性指定处理 MIME-type multipart/form-data,文件上传

struts.multipart.parser=cos

struts.multipart.parser=pell

struts.multipart.parser=jakarta

指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir

struts.multipart.saveDir=

struts.multipart.maxSize=2097152

加载自定义属性文件 (不要改写struts.properties!)

struts.custom.properties=application,org/apache/struts2/extension/custom

指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper

#struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper

指定action的后缀,默认为action

struts.action.extension=action

被 FilterDispatcher使用

如果为 true 则通过jar文件提供静态内容服务.

如果为 false 则静态内容必须位于 <context_path>/struts

struts.serve.static=true

被 FilterDispatcher使用

指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true.

struts.serve.static.browserCache=true

设置是否支持动态方法调用,true为支持,false不支持.

struts.enable.DynamicMethodInvocation = true

设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true.

struts.enable.SlashesInActionNames = false

是否允许使用表达式语法,默认为true.

struts.tag.altSyntax=true

设置当struts.xml文件改动时,是否重新加载.

- struts.configuration.xml.reload = true

设置struts是否为开发模式,默认为false,测试阶段一般设为true.

struts.devMode = false

设置是否每次请求,都重新加载资源文件,默认值为false.

struts.i18n.reload=false

###标准的UI主题

默认的UI主题为xhtml,可以为simple,xhtml或ajax

struts.ui.theme=xhtml

###模板目录

struts.ui.templateDir=template

#设置模板类型. 可以为 ftl, vm, or jsp

struts.ui.templateSuffix=ftl

###定位velocity.properties 文件. 默认 velocity.properties

struts.velocity.configfile = velocity.properties

设置velocity的context.

struts.velocity.contexts =

定位toolbox.

struts.velocity.toolboxlocation=

指定web应用的端口.

struts.url.http.port = 80

指定加密端口

struts.url.https.port = 443

设置生成url时,是否包含参数.值可以为: none, get or all

struts.url.includeParams = get

设置要加载的国际化资源文件,以逗号分隔.

struts.custom.i18n.resources=testmessages,testmessages2

对于一些web应用服务器不能处理HttpServletRequest.getParameterMap()

像 WebLogic, Orion, and OC4J等,须设置成true,默认为false.

struts.dispatcher.parametersWorkaround = false

指定freemarker管理器

#struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager

设置是否对freemarker的模板设置缓存

效果相当于把template拷贝到 WEB_APP/templates.

struts.freemarker.templatesCache=false

通常不需要修改此属性.

struts.freemarker.wrapper.altMap=true

指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false.

struts.xslt.nocache=false

设置struts自动加载的文件列表.

struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml

设定是否一直在最后一个slash之前的任何位置选定namespace.

struts.mapper.alwaysSelectFullNamespace=false

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。