Struts SwitchAction

The SwitchAction class allows you to configure multiple application modules. SwitchAction (org.apache.struts.actions.SwitchAction) is a standard Action that can switch to another module and then forward control to a path within that module.

SwitchAction expects two request parameters to be passed:

  • page: A module-relative URI (beginning with /) to which control should be forwarded after switching.
  • prefix: The module prefix (beginning with /) of the application module to which control should be switched. Use a zero-length string for the default module. The appropriate ApplicationConfig object will be stored as a request attribute, so any subsequent logic will assume the new application module.

Create a project as shown in below link.

1. Create Another Config File

Create a struts-config-module.xml file in WEB-INF folder.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
         <action path="/switchModule" forward="/success.jsp" />

2. Deployment Descriptor

Create an entry of struts-config-module.xml in web.xml as shown below


3. Create JSP file

Now create a folder module in WebContent and create a jsp file success.jsp in module folder. Write the following code.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Success Page</title>
        Login Success. Welcome <bean:write name="LoginForm" property="userName"><

4. Modify struts-config.xml

Write the following code in struts-config.xml

<action path="/switchModule" forward="/" />
<action path="/switch" type="org.apache.struts.actions.SwitchAction"/>

5. Modify login.jsp

Write the following code in login.jsp

<html:link action="switchModule">Switch Module</html:link>

6. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 9.1

Figure 9.1 Figure 9.1

Now when you click on Switch Module link, following screen will be displayed as shown in Figure 9.2

Figure 9.2 Figure 9.2

The folder structure of the example is shown below in Figure 9.3

Figure 9.3 Figure 9.3

You can download the source code of this example here.

6 Responses to Struts SwitchAction