转:Struts 2 - web.xml

The web.xml web application descriptor file represents the core of the Java web application, so it is appropriate that it is also the core of the Struts framework. In the web.xml file, Struts defines its FilterDispatcher, the Servlet Filter class that initializes the Struts framework and handles all requests. This filter can contain intialization 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 ActionCleanupFilter 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.

     

     

  • * - any other parameters are treated as framework constants.

     

     

 

Simple Example

Configuring web.xml for 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>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>

    <!-- ... -->

</web-app>

Taglib Example

Typically, configuring a taglib is neither required nor recommended. The taglib is included in struts-core.jar, and the container will discover it automatically.

If, for some reason, a taglib configuration is needed within web.xml, copy struts2-core/core/src/main/resources/META-INF/struts-tags.tld to the WEB-INF directory as struts-tags.tld. Then, add a taglib element to the web.xml.

<!-- ... -->
    </welcome-file-list>

    <taglib>
       <taglib-uri>/s</taglib-uri>
       <taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
    </taglib>
</web-app>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值