O'Reilly logo

Jakarta Struts Cookbook by Bill Siggelkow

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.7. Changing the Current Module

Problem

You want to allow the user to switch to a different module at runtime.

Solution

In the struts-config.xml file of the module that you will be switching (referred to as the "source" module), create an action that uses a type of org.apache.struts.actions.SwitchAction:

<action path="/ChangeModuleTest"
        type="org.apache.struts.actions.SwitchAction"/>

To use the action, pass request parameters that indicate the module and page within the module to switch to. The module is specified as the value of the prefix request parameter. The page is specified as the value for the page request parameter and defines the module-relative location of the resource to access.

<html:link page="/ChangeModuleTest.do?prefix=moduleName&page=/
SomeAction.do">
    Change Module
</html:link>

Since the page value is a location, if you are linking to an action, you must include the action extension (e.g., .do) as part of the parameter value.

Discussion

SwitchAction changes the application's current module to another module and forwards control to a specified module-relative URL. Like ForwardAction and IncludeAction, the SwitchAction doesn't require subclassing.

A module is an in-memory, application-relative context maintained by Struts. Modules partition a web application context into subcontexts. A module is defined by creating a separate Struts configuration XML file for the module. This file is referenced using an initialization parameter for the Struts ActionServlet in the web application's ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required