+

Search Tips   |   Advanced Search

Support multiple Struts applications


Learn how web applications that allow packaging more than one Struts-based portlet in a single application can be created either using Struts modules or a namescoped servlet context.

The Jakarta Struts Framework does not support configuring more than one Struts ActionServlet in the web deployment descriptors. This limitation occurs because there is only one servlet context per web application and the commons digester imports the Struts configuration into the servlet context. Struts stores the required information as attributes in the servlet context, and the information is retrieved using globally defined key names. Struts will suffix these attribute names with the prefix of the Struts modules, so several Struts modules are supported in a single web application, but multiple Struts applications are not.

The Struts Portlet Framework is used to create Struts applications that are deployed in WebSphere Portal. This section discusses two techniques for creating web applications that allow packaging more than one Struts-based portlet in a single application. As stated, Jakarta Struts does not support declaring the Struts ActionServlet in the web deployment descriptors more than once. The Struts Portlet Framework has the same limitation because the servlet context is used when the Struts configuration is digested in the portal as well. However, the Struts Portlet Framework supports two ways that an application can be developed in order to implement multiple Struts-based portlets. The first technique uses a single Struts controller while defining a Struts module for each portlet in the application. The second technique uses a servlet context wrapper that namescopes the attributes stored in the servlet context, so each portlet can have its own namespace in the servlet context.

Legacy portlets, such as SPFLegacyMultipleServletContext.war referenced here, are available for download from the WebSphere Portal Business Solutions Catalog.


Use Struts modules (IBM portlet container only)

One method of supporting multiple Struts portlets in a single war file is to define a Struts module for each portlet. The welcome file can be specified as a configuration parameter for each concrete portlet. This technique takes advantage of the face that the Struts Portlet Framework uses the welcome file to set the Struts module. The prefix corresponding to the Struts module for a portlet is stored in the IViewCommand object. The IViewCommand object is retrieved for each portlet and the module prefix is used to select the module, so each portlet in the application will use the Struts module selected during the welcome file processing. Obviously, the portlet can then forward to other modules as well, but initially each portlet can have a unique configuration using the Struts module.

The Struts Portlet Framework controller, WpsStrutsPortlet, is defined as the servlet-class in the web deployment descriptors. The corresponding abstract portlet is then defined in the portlet deployment descriptors. The application can then define each concrete portlet, and specify a welcome file to use. The configuration for one of the concrete portlets is demonstrated as follows:

<concrete-portlet-app uid="wp.struts.legacy.examples.MultipleStrutsApp.1">
  <portlet-app-name>Struts Legacy Multiple Modules Application 1</portlet-app-name>
  <concrete-portlet href="http://setgetweb.com/p/portal80/#Portlet_1">
    <portlet-name>Struts Legacy Multiple Modules 1</portlet-name>
      <default-locale>en</default-locale>
      <language locale="en">
        <title>Struts Legacy Multiple Modules 1</title>
        <title-short>Struts Legacy Multiple Modules 1</title-short>
        <description>Struts Legacy Multiple Modules 1</description>
        <keywords>WPS, Struts, Legacy</keywords>
      </language>
      <config-param>
        <param-name>viewMode.page</param-name>
        <param-value>portlet1/welcome.jsp</param-value>
      </config-param>
      <config-param>
        <param-name>editMode.page</param-name>
        <param-value>portlet1/edit.jsp</param-value>
      </config-param>
  </concrete-portlet>
</concrete-portlet-app>

The application can configure a separate Struts module that maps to each portlet. This scheme is customizable and will support as many Struts modules as required. However, only one abstract portlet can be defined because there can only be one Struts-based servlet-class. All of the concrete portlets created from the one abstract portlet must share the abstract's portlet configuration.

See the SPFLegacyMultipleModules example for more information.


Use a namescoped servlet context

Multiple Struts portlets in a single web application can also be supported by configuring the portlet to use a servlet context wrapper. The (Wps)StrutsPortlet class instantiates the WpsActionServlet object to digest the Struts configuration. The Wp(s)ActionServlet extends the Struts ActionServlet and uses the ActionServlet's init implementation to digest the configuration. The (Wps)StrutsPortlet object will invoke the init method of the Wp(s)ActionServlet to digest the configuration. Since the (Wps)StrutsPortlet instantiates the Wp(s)ActionServlet and calls the init method, the Struts Portlet Framework has the opportunity to provide a servlet context wrapper. If the application is configured to use the servlet context wrapper, then the Wp(s)ActionServlet will return the servlet context wrapper when the getServletContext method is invoked. The servlet context wrapper defines a namespace unique to the portlet and then stores the attributes in the real servlet context. The concept is similar to how Struts prefixes the attributes in the servlet context for the configuration of each Struts module.

The Wp(s)ActionServlet should be used to obtain the servlet context wrapper when this technique is used. The servlet context obtained from the pageContext, for example, will not be the wrappered servlet context. The Wp(s)ActionServlet can be obtained from the request object using the Globals.ACTION_SERVLET_KEY.


Parent: Struts Portlet Framework