Struts2 web.xml

本文详细介绍了Struts框架的核心配置文件web.xml的作用及其配置方法。包括FilterDispatcher控制器、ActionContextCleanUp类的功能介绍,以及如何通过初始化参数进行框架配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Theweb.xmlweb application descriptor(描述符) file represents(表示) the core(核心) of the Java web application, so it is appropriate(适当的) that it is also part(部分) of the core of the Struts framework. In theweb.xmlfile, Struts defines its FilterDispatcher(控制器),the Servlet Filter class that initializes the Struts framework and handles(处理) all requests. This filter can contain(包含) initialization parameters that affect(影响) what, if any, additional(额外的) configuration files are loaded and how the framework should behave.

In addition to the FilterDispatcher, Struts also provides(提供) an ActionContextCleanUp class that handles special(特殊) cleanup tasks(工作) when other filters, such as those used by Sitemesh, need access(使用) to an initialized Struts framework.

Key Initialization Parameters

config- a comma-delimited(逗号分割) list of XML configuration files to load.

actionPackages- a comma-delimited list of Java packages to scan(浏览) for Actions.

configProviders- a comma-delimited list of Java classes that implement the ConfigurationProvider interface that should be used for building(构建) the Configuration.

loggerFactory- The class name of the LoggerFactory implementation.

*- any other parameters are treated(对待) as framework constants(常量).

Simple Example

Configuringweb.xmlfor the framework is a matter of adding a filter and filter-mapping.

FilterDispatcher Example (web.xml)
<web-app id="WebApp_9" version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
        	<param-name>actionPackages</param-name>
        	<param-value>com.mycompany.myapp.actions</param-value>
        </init-param>
    </filter>

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

    <!-- ... -->

</web-app>
Changed Filter Structure in Struts >= 2.1.3
To split up the the dispatcher phases, FilterDispatcher is deprecated since Struts 2.1.3. If working with older versions, you need to use
    ...
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    ...

SeeSiteMesh Pluginfor an example on when to use seperate Filters for prepare and execution phase

Why the Filter is mapped with /* and how to configure explicit exclusions(除外) (since 2.1.7)
In the example above(上文) we've mapped the Struts 2 dispatcher(分配器) to/*, so Struts 2 has a crack(打开) at all incoming(进来) requests. This is because Struts 2 serves static content from its jar files, including Dojo JavaScript files (if using S2.0, or the Dojo plugin in S2.1+) and FreeMarker(模板技术) templates for the Struts 2 tags that produce(产生) HTML.

If we change the filter mapping to something else, for example/*.html, we must take(获得) this in to account and extract(提取) the content that would normally(通常地) be served from the Struts 2 jar files, or some other solution(解决方案).

Since(由于) Struts 2.1.7, you are able to provide a comma(逗号) seperated list of patterns for which when matching against the
request URL the Filter will just pass by. This is done via the configuration option struts.action.excludePattern, for example in your struts.xml

<struts>
    <constant name="struts.action.excludePattern" value=".*unfiltered.*,.*\\.nofilter"/>
    ...

</struts>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值