Struts2_配置文件

来源:互联网 发布:java 高德地图api 编辑:程序博客网 时间:2024/05/16 17:40

当世界给草籽重压时,它总会用自己的方法破土而出。—我们共勉

Struts2框架中核心组件就是Action、拦截器等,Struts2框架使用包来管理Action和拦截器等。每个包就是多个Action、多个拦截器、多个拦截器引用的集合。下面一起看一下几个struts的配置文件。最核心的是web.xml和struts.xml。

1.web.xml

通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于Struts2框架而言,需要加载FilterDispatcher,只要Web应用负责加载FilterDispatcher,FilterDispatcher将会加载Struts2框架。 因为Struts2将核心控制器设计成Filter,而不是一个普通Servlet。故为了让Web应用加载FilterDispatcher,只需要在web.xml文件中配置FilterDispatcher即可。

位置:WebContent/WEB-INF/web.xml

看一下web.xml 的配置文件:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <filter>   <!-- Struts2核心Filter的名字 -->      <filter-name>ApacheStruts2</filter-name>    <!-- Struts2核心Filter的实现类 -->      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>     <init-param>                <!-- Struts2框架默认加载的Action包结构,可以没有。 -->                <param-name>actionPackages</param-name>                <param-value>org.apache.struts2.showcase.person</param-value>            </init-param>            <!-- Struts2框架的配置提供者类 -->            <init-param>                <param-name>configProviders</param-name>                <param-value>lee.MyConfigurationProvider</param-value>            </init-param>    </filter>  <!-- 配置Filter拦截的URL -->    <filter-mapping>   <!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->      <filter-name>ApacheStruts2</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>

详细说下:

<filter>          <filter-name>struts2</filter-name>          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  

定义filter,此处加载struts2过滤器。

<filter-mapping>          <filter-name>struts2</filter-name>          <url-pattern>/*</url-pattern>  </filter-mapping> 

定义web服务器通知项,即,符合“/*”的URL请求都交给命名为“struts2”的过滤器(即,org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)处理。

2.struts.xml

干什么的:struts.xml文件主要负责管理应用中的Action映射, 及Action处理结果和物理资源之间的映射关系。

放在哪里:只能放在src文件下。

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    "http://struts.apache.org/dtds/struts-2.5.dtd"><struts><!--常量的基础配置  -->      <constant name="struts.enable.DynamicMethodInvocation" value="false" />      <constant name="struts.devMode" value="true" />      <constant name="struts.i18n.reload" value="true" />      <constant name="struts.configuration.xml.reload" value="true" ></constant>  <package name="user" namespace="/user" extends="struts-default">    <!-- 定义处理请求URL为loginAction的Action -->    <action name="login" class="com.lee.action.LoginAction">        <!-- 定义处理结果字符串和资源之间的映射关系 -->        <result name="成功" type="dispatcher">/WEB-INF/main.jsp</result>        <result name="失败" type="dispatcher">/index.jsp</result>    </action></package></struts>

详细说一下:

重要:<action name="" method="" class="">  <result name="" type=""></result>  </action>  

action:
name: 请求名称,加上namespace就是请求路径。
method: 接到请求后执行的方法,默认执行execute(此处表示没有method属性)。
class: 处理方法所在的类,默认为Action(如果没有配置此属性)。
还有一个不常用的属性,converter,Action的类型转换器。
result:
name: Action返回结果,默认为SUCCESS(“success”).
type: 返回的类型,默认为dispatcher。

1.<constantnameconstantname="struts.devMode"value="true"/>  

开发阶段配置,在web服务器出错时会尽量打印出来,一样也是生产阶段设置为false,避免后台结构被人发现。

2.<constant name="struts.configuration.xml.reload"value="true"/> 

配置文件修改后是否自动重新部署到服务器,开发阶段需要设置为true,否则需要不时重启服务器。

3.constantnameconstantname="struts.custom.i18n.resources"value="globalMessages"/> 

加载国际化配置文件,多个配置文件则用“,”隔开。

4.<constantnameconstantname="struts.action.extension"value="action,,"/>  

默认请求后缀,多个使用”,”隔开。

5.<constant name="struts.i18n.encoding" value="UTF-8" />

指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法。

6.<constant name="struts.multipart.maxSize" value="2097152"/> 

multipart请求信息的最大尺寸(文件上传用,该属性指定Struts 2文件上传中整个请求内容允许的最大字节数)。

3.struts-default.xml

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

位置:在struts2-core-2.5.8.jar包中。

主要了解一下package中一下内容:

(1).result-types

<!-- 结果类型的种类-->  <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>  

(2).interceptors

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

(3).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="params"/>  <interceptor-ref name="conversionError"/>  </interceptor-stack>  

(4).bean

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

(5).定义struts默认包

<!-- 定义Struts2默认包-->  <package name="struts-default" abstract="true">  

4.default.properties

地位:默认常量配置

位置:struts.core/org.apache.struts2/default.properties

名称 作用 struts.i18n.encoding=UTF-8 编码格式配置 struts.objectFactory = spring SSH集成时使用 struts.objectFactory.spring.autoWire = name 自动装配 struts.multipart.parser=jakarta 文件上传相关配置 struts.multipart.saveDir= saveDir:上传的临时目录 struts.multipart.maxSize=2097152 maxSize:文件的最大值 struts.action.extension=action,, URL后缀配置,可以自定义。支持/xxxaction和没有后缀/xxx struts.enable.DynamicMethodInvocation = false 动态方法调用

5.struts.properties

struts.properties 是可以不要的!!!
因为struts.xml文件中有<constant>这个节点,你可以把你想写在struts.properties的自定义配置写在 struts.xml文件当中.

在struts2中默认处理的请求后缀为action,或.do,我们可以修改struts.xml 和struts.properties来修改默认的配置。
1、在struts.xml中添加子结点

<constant name="struts.action.extension" value="action,do,webwork" />

2、或者是修改struts.properties文件,添加

struts.action.extension = action,do

这里写图片描述

原创粉丝点击