打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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 part of 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 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

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

    Icon

    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>
    ...

    See SiteMesh Plugin for 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)

    Icon

    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>

    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.

    (tick) If, for some reason, a taglib configuration is needed within web.xml, extract the TLD file from the struts-core.jar META-INF folder, and 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>

    Custom FileManager and FileManagerFactory implementations

    If there is a need to support an App Server's specific file system (eg. VFS in JBoss), you can implement your own version of FileManager. But it must be registered at "the beginning" to support bootstrap of the whole framework.

    To register your own FileManger you can do it with <init-param/> as below:

    <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         <init-param>
             <param-name>struts.fileManager</param-name>
             <param-value>com.company.MyFileManager</param-value>
         </init-param>
    </filter>

    You can as well register your own FileManagerFactory with <init-param/>, see example:

    <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
         <init-param>
             <param-name>struts.fileManagerFactory</param-name>
             <param-value>com.company.MyFileManagerFactory</param-value>
         </init-param>
    </filter>

    Take a look on default implementations - DefaultFileManager.java and DefaultFileManagerFactory.java to understand how and why.

    本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
    打开APP,阅读全文并永久保存 查看更多类似文章
    猜你喜欢
    类似文章
    【热】打开小程序,算一算2024你的财运
    Struts2.0配置文件(web.xml)
    struts2的小知识
    struts2 处理请求流程分析(结合源码)1
    JSTL与Struts的结合(十一)
    SSH框架 web.xml配置
    在Struts2中实现系统的初始化工作
    更多类似文章 >>
    生活服务
    热点新闻
    分享 收藏 导长图 关注 下载文章
    绑定账号成功
    后续可登录账号畅享VIP特权!
    如果VIP功能使用有故障,
    可点击这里联系客服!

    联系客服