Using Servlet Mapping with Struts 2

April 22, 2013

I had a struts 2 web application, and I wanted to call some code that was implemented as a pure java servlet. Struts 2 uses a servlet filter:

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

and I wanted to setup a new servlet that matched by url:

<servlet-mapping>
   <servlet-name>cardInterface</servlet-name>
    <url-pattern>/card/*</url-pattern>
</servlet-mapping>

This didn't work - requests for /card/* were handled by the struts 2 filter, and treated as a struts 2 action.

Solution

Struts 2 has support for ignoring requests for static content - I managed to use this feature to tell struts 2 to ignore the /card/* url.

At the bottom of struts.xml I added the following constant:

<constant name="struts.action.excludePattern" value="/card/.*?" />

References