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.

5 Responses to Struts SwitchAction

  1. Divakar Ingilala December 19, 2012 at 9:16 am #

    Nice post…thank you

    • Jagadeesh February 15, 2013 at 6:32 pm #

      Nice and easy to understand……..good effort….Thanks a lot.

      • chiru December 1, 2014 at 12:33 pm #

        IN real time project which action we going to use plz give me answer……

  2. RN. Ayaansita May 4, 2013 at 1:17 am #

    one of the simple attempt and good effort. But I’d like to the address bar be look like


    instead of

    where “StrutsSwitchAction” is the default module and “module” is the module after switching.

    Thank you for noticing my question.

  3. chiru December 1, 2014 at 12:31 pm #

    nice post

Leave a Reply