Configuring Struts 2 in WebSphere Application Server for Web Server Plugin

It has been some time since I am submitting a post. A critical project in crisis was requiring my undivided attention. The project was a web application using Struts 2 web framework to be deployed on Web Application Server 6.1.

The web.xml configuration for Struts has changed considerably from Struts 1.x to Struts 2. In Struts 1.x the web.xml had the following entries.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

  <!-- Action Servlet Configuration -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
    <param-name>application</param-name>
    <param-value>test.struts.MessageResources</param-value>
  </init-param>
    <init-param>
      <param-name>mapping</param-name>
      <param-value>org.apache.struts.action.RequestActionMapping</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <!-- Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- Struts Tag Library Descriptors -->
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-template.tld</taglib-location>
  </taglib>

</web-app>

The rest of the web.xml structure is not important, the key is to define the Struts 1.x ActionServlet and the servlet url mapping. Now in Struts 2, the framework has completely removed the ActionServlet definition and instead defines a servlet filter and its filter mapping. Refer the snippet of Struts 2 web.xml.

<filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
</filter-mapping>

Now WebSphere provides a ready-to-use plugin.xml generator which can be configured in Web Server httpd.conf. Unfortunately this plugin does not pick up the filter mappings defined in the web.xml.

So a workaround for that is to define your own custom servlet which does nothing except providing a redirect to the Struts 2 filter. Here’s the sample web.xml snippet.

<servlet-mapping>
	<servlet-name>RoutingServlet</servlet-name>
	<url-pattern>*.action</url-pattern>
</servlet-mapping>

<filter>
	<filter-name>struts2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
	<filter-name>struts2</filter-name>
	<servlet-name>RoutingServlet</servlet-name>
</filter-mapping>

The existing Struts 2 filter mapping is commented out.

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

This code generates the required code in the WebSphere plugin.xml file and also routes the HTTP requests via RoutingServlet to the Struts 2 filter.

Advertisements

5 thoughts on “Configuring Struts 2 in WebSphere Application Server for Web Server Plugin

  1. Interesting idea, but it seems to be an overkill to workaround a deficiency in a product (WebSphere).

    1. True. But once your client has invested in the WebSphere stack there is not much you can do but provide a workaround.

    1. Filters are invoked in two ways. One by mapping the filter to a url pattern and second by associating with a servlet. Details are explained in this url or this one.

      I hope that I have responded to your query.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s