Struts2中struts-default.xml、struts.xml及struts.properties详解

1)struts-default.xml

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

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

<!DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

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

<struts>

<!--struts2中工厂bean的定义-->

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

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

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

<beantype="com.opensymphony.xwork2.ActionProxyFactory"name="struts"class="org.apache.struts2.impl.StrutsActionProxyFactory"/>

<!--类型检测bean的定义-->

<beantype="com.opensymphony.xwork2.util.ObjectTypeDeterminer"name="tiger"class="com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer"/>

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

<beantype="com.opensymphony.xwork2.util.ObjectTypeDeterminer"name="struts"class="com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer"/>

<!--文件上传bean的定义-->

<beantype="org.apache.struts2.dispatcher.mapper.ActionMapper"name="struts"class="org.apache.struts2.dispatcher.mapper.DefaultActionMapper"/>

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

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

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

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

<beantype="org.apache.struts2.dispatcher.multipart.MultiPartRequest"name="jakarta"class="org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest"scope="default"optional="true"/>

<!--标签库bean的定义-->

<beantype="org.apache.struts2.views.TagLibrary"name="s"class="org.apache.struts2.views.DefaultTagLibrary"/>

<!--一些常用视图bean的定义-->

<beanclass="org.apache.struts2.views.freemarker.FreemarkerManager"name="struts"optional="true"/>

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

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

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

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

<beantype="org.apache.struts2.components.template.TemplateEngine"name="jsp"class="org.apache.struts2.components.template.JspTemplateEngine"/>

<!--类型转换bean的定义-->

<beantype="com.opensymphony.xwork2.util.XWorkConverter"name="xwork1"class="com.opensymphony.xwork2.util.XWorkConverter"/>

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

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

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

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

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

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

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

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

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

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

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

<beanclass="org.apache.struts2.views.util.UrlHelper"static="true"/>

<!--定义Struts2默认包-->

<packagename="struts-default"abstract="true">

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

<result-types>

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

<result-typename="dispatcher"class="org.apache.struts2.dispatcher.ServletDispatcherResult"default="true"/>

<result-typename="freemarker"class="org.apache.struts2.views.freemarker.FreemarkerResult"/>

<result-typename="httpheader"class="org.apache.struts2.dispatcher.HttpHeaderResult"/>

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

<result-typename="redirectAction"class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

<result-typename="stream"class="org.apache.struts2.dispatcher.StreamResult"/>

<result-typename="velocity"class="org.apache.struts2.dispatcher.VelocityResult"/>

<result-typename="xslt"class="org.apache.struts2.views.xslt.XSLTResult"/>

<result-typename="plainText"class="org.apache.struts2.dispatcher.PlainTextResult"/>

<result-typename="redirect-action"class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

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

</result-types>

<!--struts2中拦截器的定义-->

<interceptors>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<!--扩展引用-->

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

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

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

<!--异常处理-->

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

<!--文件上传,解析表单域的内容-->

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

<!--支持国际化-->

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

<!--日志记录-->

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

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

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

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

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

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

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

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

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

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

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

<!--范围转换-->

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

<!--用于访问ServletAPI-->

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

<interceptor-stackname="basicStack">

<interceptor-refname="exception"/>

<interceptor-refname="servletConfig"/>

<interceptor-refname="prepare"/>

<interceptor-refname="checkbox"/>

<interceptor-refname="params"/>

<interceptor-refname="conversionError"/>

</interceptor-stack>

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

<interceptor-stackname="validationWorkflowStack">

<interceptor-refname="basicStack"/>

<interceptor-refname="validation"/>

<interceptor-refname="workflow"/>

</interceptor-stack>

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

<interceptor-stackname="fileUploadStack">

<interceptor-refname="fileUpload"/>

<interceptor-refname="basicStack"/>

</interceptor-stack>

<!--model-driven栈-->

<interceptor-stackname="modelDrivenStack">

<interceptor-refname="modelDriven"/>

<interceptor-refname="basicStack"/>

</interceptor-stack>

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

<interceptor-stackname="chainStack">

<interceptor-refname="chain"/>

<interceptor-refname="basicStack"/>

</interceptor-stack>

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

<interceptor-stackname="i18nStack">

<interceptor-refname="i18n"/>

<interceptor-refname="basicStack"/>

</interceptor-stack>

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

<interceptor-stackname="paramsPrepareParamsStack">

<interceptor-refname="exception"/>

<interceptor-refname="alias"/>

<interceptor-refname="params"/>

<interceptor-refname="servletConfig"/>

<interceptor-refname="prepare"/>

<interceptor-refname="i18n"/>

<interceptor-refname="chain"/>

<interceptor-refname="modelDriven"/>

<interceptor-refname="fileUpload"/>

<interceptor-refname="checkbox"/>

<interceptor-refname="staticParams"/>

<interceptor-refname="params"/>

<interceptor-refname="conversionError"/>

<interceptor-refname="validation">

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

</interceptor-ref>

<interceptor-refname="workflow">

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

</interceptor-ref>

</interceptor-stack>

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

<interceptor-stackname="defaultStack">

<interceptor-refname="exception"/>

<interceptor-refname="alias"/>

<interceptor-refname="servletConfig"/>

<interceptor-refname="prepare"/>

<interceptor-refname="i18n"/>

<interceptor-refname="chain"/>

<interceptor-refname="debugging"/>

<interceptor-refname="profiling"/>

<interceptor-refname="scopedModelDriven"/>

<interceptor-refname="modelDriven"/>

<interceptor-refname="fileUpload"/>

<interceptor-refname="checkbox"/>

<interceptor-refname="staticParams"/>

<interceptor-refname="params">

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

<interceptor-stackname="completeStack">

<interceptor-refname="defaultStack"/>

</interceptor-stack>

<interceptor-stackname="executeAndWaitStack">

<interceptor-refname="execAndWait">

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

</interceptor-ref>

<interceptor-refname="defaultStack"/>

<interceptor-refname="execAndWait">

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

</interceptor-ref>

</interceptor-stack>

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

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

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

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

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

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

</interceptors>

<!--定义默认拦截器为"defaultStack"-->

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

</package>

</struts>

2)struts.xml

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

<?xmlversion="1.0"encoding="GBK"?>

<!--下面指定Struts2配置文件的DTD信息-->

<!DOCTYPEstrutsPUBLIC

"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"

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

<!--struts是Struts2配置文件的根元素-->

<struts>

<!--下面元素可以出现0次,也可以无限多次-->

<constantname=""value=""/>

<!--下面元素可以出现0次,也可以无限多次-->

<beantype=""name=""class=""scope=""static=""optional=""/>

<!--下面元素可以出现0次,也可以无限多次-->

<includefile=""/>

<!--package元素是Struts配置文件的核心,该元素可以出现0次,或者无限多次-->

<packagename="必填的包名"extends=""namespace=""abstract=""

externalReferenceResolver>

<!--该元素可以出现,也可以不出现,最多出现一次-->

<result-types>

<!--该元素必须出现,可以出现无限多次-->

<result-typename=""class=""default="true|false">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</result-type>

</result-types>

<!--该元素可以出现,也可以不出现,最多出现一次-->

<interceptors>

<!--该元素的interceptor元素和interceptor-stack至少出现其中之一,

也可以二者都出现-->

<!--下面元素可以出现0次,也可以无限多次-->

<interceptorname=""class="">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</interceptor>

<!--下面元素可以出现0次,也可以无限多次-->

<interceptor-stackname="">

<!--该元素必须出现,可以出现无限多次-->

<interceptor-refname="">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</interceptor-ref>

</interceptor-stack>

</interceptors>

<!--下面元素可以出现0次,也可以无限多次-->

<default-interceptor-refname="">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>

</default-interceptor-ref>

<!--下面元素可以出现0次,也可以无限多次-->

<default-action-refname="">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</default-action-ref>?

<!--下面元素可以出现0次,也可以无限多次-->

<global-results>

<!--该元素必须出现,可以出现无限多次-->

<resultname=""type="">

<!--该字符串内容可以出现0次或多次-->

映射资源

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</result>

</global-results>

<!--下面元素可以出现0次,也可以无限多次-->

<global-exception-mappings>

<!--该元素必须出现,可以出现无限多次-->

<exception-mappingname=""exception=""result="">

异常处理资源

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</exception-mapping>

</global-exception-mappings>

<actionname=""class=""method=""converter="">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

<!--下面元素可以出现0次,也可以无限多次-->

<resultname=""type="">

映射资源

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</result>

<!--下面元素可以出现0次,也可以无限多次-->

<interceptor-refname="">

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</interceptor-ref>

<!--下面元素可以出现0次,也可以无限多次-->

<exception-mappingname=""exception=""result="">

异常处理资源

<!--下面元素可以出现0次,也可以无限多次-->

<paramname="参数名">参数值</param>*

</exception-mapping>

</action>

</package>*

<struts>

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,andconstructor(name是默认装配模式)

struts.objectFactory.spring.autoWire=name

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

struts.objectFactory.spring.useClassCache=true

###指定类型检查

#struts.objectTypeDeterminer=tiger

#struts.objectTypeDeterminer=notiger

###该属性指定处理MIME-typemultipart/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,orjsp

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,getorall

struts.url.includeParams=get

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

#struts.custom.i18n.resources=testmessages,testmessages2

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

###像WebLogic,Orion,andOC4J等,须设置成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

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

struts.xslt.nocache=false

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

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

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

struts.mapper.alwaysSelectFullNamespace=false

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yaolong77/archive/2009/03/10/3975657.aspx