学过框架的人应该都知道,框架的最大特点就是使用配置文件,当然Struts2也不例外,今天讲解的主要是Struts2在使用中会常用涉及到的一些配置文件。
先来说一下下面几个配置文件的关系:
在default.properties中的struts.configuration.files 属性指定Struts 2框架默认加载的配置文件,该属性的默认值为struts-default.xml,struts-plugin.xml,struts.xml(三者的加载顺序就是struts-default.xml,struts-plugin.xml,struts.xml,跟配置顺序有关)。
1.struts-default.xml(系统提供)
这个文件是struts2框架默认加载的配置文件。它定义struts2一些核心的bean和拦截器;下面简要看一下它的代码:
<?xml version="1.0"encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <bean class="com.opensymphony.xwork2.ObjectFactory"name="xwork" /> <bean type="com.opensymphony.xwork2.ObjectFactory"name="struts" class="org.apache.struts2.impl.StrutsObjectFactory"/> …… <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-types> <interceptors> <interceptor name="alias"class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/> <interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/> …… <!-- Basic stack --> <interceptor-stack name="basicStack"> <interceptor-refname="exception"/> <interceptor-refname="servletConfig"/> …… </interceptor-stack> …… </interceptors> <default-interceptor-ref name="defaultStack"/> <default-class-refclass="com.opensymphony.xwork2.ActionSupport" /> </package> </struts>
2.struts-plugin.xml(系统提供)Struts2的plugin为Struts2的主程序本身提供额外的功能支持(位于struts2-spring-plugin-2.3.1.2.jar下),同样看一下它的大体代码:
<?xml version="1.0"encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <bean type="com.opensymphony.xwork2.ObjectFactory"name="spring"class="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <!-- 将struts的objectFactory的值默认设置为spring, 所以使用struts2-spring-plugin.jar包时 需要在struts.xml中更改struts.objectFactory的值 --> <constant name="struts.objectFactory"value="spring" /> <constant name="struts.class.reloading.watchList"value="" /> <constant name="struts.class.reloading.acceptClasses"value="" /> <constant name="struts.class.reloading.reloadConfig"value="false" /> <package name="spring-default"> <interceptors> <interceptor name="autowiring"class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/> </interceptors> </package> </struts>
3.struts.xml(自己编写)
该文件也是struts2框架自动加载的文件,在这个文件中可以定义一些自己的action, interceptor,package等,该文件的package 通常继承struts-default包。
<?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD StrutsConfiguration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--constant标签可以配置struts.properties中定义的属性 将struts中的文件交给spring来管理,需要struts2-spring-plugin.jar的支持--> <constant name="struts.objectFactory" value="spring"/> <!-- package标签就是包,用于区分名称, namespace可以更改访问路径,一般都继承struts-default包 --> <package name="testlogin" namespace="/"extends="struts-default"> <!-- interceptors标签用于注册拦截器或者拦截器栈 --> <interceptors> <!-- 注册拦截器 --> <interceptor name="myInterceptor" class="myInterceptor"></interceptor> <!-- 注册拦截栈 --> <interceptor-stack name="defaultInterceptorStack"> <interceptor-ref name="myInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 设置整个包范围内所有Action所要应用的默认拦截器 --> <default-interceptor-ref name="defaultInterceptorStack"></default-interceptor-ref> <!-- 注册action,class的具体类名在spring中的配置文件中注册 --> <action name="login" class="loginAction"> <result name="success">/success.jsp</result> <result name="error" type="redirect">/error.jsp</result> </action> </package> </struts>
4.default.properties(系统提供)
这个文件是struts2框架的全局属性文件,也是自动加载的文件。该文件包含了系列的key-value对。该文件完全可以配置在struts.xml文件中,使用constant元素。由于该文件里的格式都是一样的,我就先列举两个:
struts.i18n.encoding=UTF-8 struts.objectFactory.spring.autoWire = name ……
5.web.xml(自己编写)在web.xml中配置strut2,如果是2.1.3之前的版本,用org.apache.struts2.dispatcher.FilterDispatcher,以后的版本则配置org.apache.struts2.dispatcher.ng.filter.StrutsPreparedAndExecuteFilter。
一般情况下,只需要配置一下filter即可,但是如果struts.xml发生更改(如目录,名字等),则需要在filter下再配置参数,参数值要依次写上struts-default.xml,struts-plugin.xml,struts.xml(如果是更改目录则需要填写struts.xml更改后的目录)。
下面的例子我使用的是strut2.3.1.2,所以使用的是StrutsPreparedAndExecuteFilter。
<!-- 配置struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 更改struts.xml的目录,放到config文件夹下 --> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
我们对这几个配置文件的概念有所了解后,使用起Struts2来就不会再迷迷糊糊的了。