每学习一个框架,我们都免不了要学习一些关于的配置文件,struts2也不例外,下面我就讲一下struts2中几个主要的配置文件。
1) struts-default.xml
这个文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--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的定义--> <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的定义--> <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的定义--> <bean type="org.apache.struts2.views.TagLibrary" name="s" class="org.apache.struts2.views.DefaultTagLibrary" /> <!--一些常用视图bean的定义--> <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的定义--> <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" />
<!-- 定义Struts2默认包--> <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>
<!--struts2中拦截器的定义--> <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> <!--定义默认拦截器为"defaultStack"--> <default-interceptor-ref name="defaultStack"/> </package> </struts>