struts2的配置文件
web.xml:/WEB-INF/ Web部署描述符,包括所有必需的框架组件
struts.xml:/WEB-INF/classes/ 主要的配置文件,包含result映射、action映射、拦截器配置等
struts.properties:/WEB-INF/classes/ struts2框架属性
struts-default.xml:/WEB-INF/lib/struts2-core.jar struts2提供默认配置
struts-plugin.xml:/WEB-INF/lib/struts2-xxx-plugin.jar struts2框架的插件所用的配置文件
web.xml
struts2框架需要你在web.xml文件中配置一个前端控制器——FilterDispatcher,用于对Struts框架进行初始化以及处理所有的请求。
FilterDispatcher可以包含一些初始化的参数
config:要加载的XML配置文件列表(以逗号分隔)。如果没有设置这个参数,struts2框架默认将加载struts-default.xml、struts-plugin.xml和struts.xml
actionPackages:以逗号分隔的Java包名的列表,struts2框架将扫描这些包中的Action类。
configProviders:实现了ConfigurationProvider接口的Java类的列表(以逗号分隔),ConfiguratioonProvider接口描述了框架的配置,默认情况下,struts2框架使用StrutsXmlConfigurationProvider从XML文档中加载他的配置
例如:
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.mycompany.myapp.actions</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts-default.xml
struts-default.xml是struts2框架的基础配置文件,位于struts-core-2.0.11.jar中,struts-default.xml文件会自动被包含到struts.xml文件中,以提供标准的配置设置而不需要复制其内容,例如我们在配置strtus.xml时:
<package name="default" extends="struts-default" >
struts-default包就是在struts-default.xml文件中定义的,在这个包中定义了struts2内置的结果类型(包括Servlet转发、Servlet重定向、FreeMarker模板输出、XSTL渲染和ActionChainResult等),内置的拦截器以及由不同拦截器组成的拦截器栈,这些拦截器可以直接使用,也可以作为自定义的拦截器栈的基础。
struts.xml
struts.xml是struts2框架的核心配置文件,在这个配置文件中可以配置作用于action的拦截器、action和result的映射等。
struts.xml文件的元素结构
struts2给出了struts.xml文件的dtd(document type definition,文档类型定义)在struts2的核心类型中有一个struts-2.0.dtd文件,该文件就是struts.xml和struts-default.xml文件的dtd
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* $Id: struts-2.0.dtd 651946 2008-04-27 13:41:38Z apetrelli $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<!-- START SNIPPET: strutsDtd -->
<!--
Struts configuration DTD.
Use the following DOCTYPE
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
-->
<!ELEMENT struts (package|include|bean|constant)*>
<!ELEMENT package (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?, default-class-ref?, global-results?, global-exception-mappings?, action*)>
<!ATTLIST package
name CDATA #REQUIRED
extends CDATA #IMPLIED
namespace CDATA #IMPLIED
abstract CDATA #IMPLIED
externalReferenceResolver NMTOKEN #IMPLIED
>
<!ELEMENT result-types (result-type+)>
<!ELEMENT result-type (param*)>
<!ATTLIST result-type
name CDATA #REQUIRED
class CDATA #REQUIRED
default (true|false) "false"
>
<!ELEMENT interceptors (interceptor|interceptor-stack)+>
<!ELEMENT interceptor (param*)>
<!ATTLIST interceptor
name CDATA #REQUIRED
class CDATA #REQUIRED
>
<!ELEMENT interceptor-stack (interceptor-ref*)>
<!ATTLIST interceptor-stack
name CDATA #REQUIRED
>
<!ELEMENT interceptor-ref (param*)>
<!ATTLIST interceptor-ref
name CDATA #REQUIRED
>
<!ELEMENT default-interceptor-ref (#PCDATA)>
<!ATTLIST default-interceptor-ref
name CDATA #REQUIRED
>
<!ELEMENT default-action-ref (#PCDATA)>
<!ATTLIST default-action-ref
name CDATA #REQUIRED
>
<!ELEMENT default-class-ref (#PCDATA)>
<!ATTLIST default-class-ref
class CDATA #REQUIRED
>
<!ELEMENT global-results (result+)>
<!ELEMENT global-exception-mappings (exception-mapping+)>
<!ELEMENT action (param|result|interceptor-ref|exception-mapping)*>
<!ATTLIST action
name CDATA #REQUIRED
class CDATA #IMPLIED
method CDATA #IMPLIED
converter CDATA #IMPLIED
>
<!ELEMENT param (#PCDATA)>
<!ATTLIST param
name CDATA #REQUIRED
>
<!ELEMENT result (#PCDATA|param)*>
<!ATTLIST result
name CDATA #IMPLIED
type CDATA #IMPLIED
>
<!ELEMENT exception-mapping (#PCDATA|param)*>
<!ATTLIST exception-mapping
name CDATA #IMPLIED
exception CDATA #REQUIRED
result CDATA #REQUIRED
>
<!ELEMENT include (#PCDATA)>
<!ATTLIST include
file CDATA #REQUIRED
>
<!ELEMENT bean (#PCDATA)>
<!ATTLIST bean
type CDATA #IMPLIED
name CDATA #IMPLIED
class CDATA #REQUIRED
scope CDATA #IMPLIED
static CDATA #IMPLIED
optional CDATA #IMPLIED
>
<!ELEMENT constant (#PCDATA)>
<!ATTLIST constant
name CDATA #REQUIRED
value CDATA #REQUIRED
>
<!-- END SNIPPET: strutsDtd -->
struts-plugin.xml
struts2提供了类型于Eclipse的插件机制来扩展自身功能,插件以Jar包的方式提供
下面我们看一下Spring插件的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
* $Id: struts-plugin.xml 1221225 2011-12-20 12:22:28Z jogep $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<!-- Make the Spring object factory the automatic default -->
<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>