- Define Struts action servlet and its initialization parameters.
- Specify servlet mapping for the action servlet. There are two types of mapping which delegate the matching URLs to be processed by Struts controller:
<servlet> <servlet-name>StrutsController</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>We also declare an initialization parameter named config– which points to the location of Struts configuration files. Multiple configuration files can be used, and they must be separated by comma, for example:
<init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config1.xml, /WEB-INF/struts-config2.xml </param-value> </init-param>Beside the commonly used parameter config, Struts also defines several parameters for configuring its controller: chainConfig, config/${module}, configFactory, convertNull, rulesets, validating.
<servlet-mapping> <servlet-name>StrutsController</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>The URL pattern /services/*is called prefix matching which means that all URLs start with the prefix will be processed by the action servlet. For example, the following URLs will match the pattern:
http://localhost/webapp/services/deposit
http://localhost/webapp/services/widthdraw
The second pattern is called extension matching which means that all URLs end with the extension will be processed by the action servlet, for example:<servlet-mapping> <servlet-name>StrutsController</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>The following example URLs will match the extension matching pattern:
http://localhost/webapp/login.do
http://localhost/webapp/listProduct.do
So far a typical configuration for Struts 1 framework in web.xml file looks like this:<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" 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"> <display-name>Struts1Application</display-name> <!-- Begin Struts 1 config --> <servlet> <servlet-name>StrutsController</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StrutsController</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- End Struts 1 config --> </web-app>
- Declaration of the filter dispatcher.
- URL mapping for the filter dispatcher.
And the following is a typical configuration for enabling Struts 2 in the web.xml file:<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" 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"> <display-name>Strut2Application</display-name> <!-- Begin Struts 2 config --> <filter> <filter-name>DispatcherFilter</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>DispatcherFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- End Struts 2 config --> </web-app>