Child pages
  • web.xml
Skip to end of metadata
Go to start of metadata

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.

Simple Example

Configuring web.xml for the framework is a matter of adding a filter and filter-mapping.

Filter Example (web.xml) struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /* ]]>

Changed filter package in Struts >= 2.5

As from Struts 2.5 all filters were moved to top package, if you are using older version you must use the old package, see example:

xml struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter ...]]>

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

XML struts2 org.apache.struts2.dispatcher.FilterDispatcher ... ]]>

See SiteMesh Plugin for an example on when to use seperate Filters for prepare and execution phase

Exclude specific URLs

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

 

XML ... ]]>

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.

XML /s /WEB-INF/struts-tags.tld ]]>

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:

xml struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts.fileManager com.company.MyFileManager ]]>

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

xml struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts.fileManagerFactory com.company.MyFileManagerFactory ]]>

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

Custom configuration provider

It is possible to use your custom ConfigurationProvider to programmatically configure your application. To do this use configProviders (it can be a comma-separated list of class names) <init-param/> as below:

struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter configProviders com.company.MyConfigurationProvider ]]>

See Configuration Provider & Configuration for more details.

 

1 Comment

  1. I found that when placing the struts jar files into the APP-INF/lib directory under BEA WebLogic, I had to extract the struts-tags.tld file out of the jar file and place it into the WEB-INF/classes directory of the war file in order for the WebLogic server to find the tag libraries with the 2.4 version of the web.xml DTD.