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.

http://kruders.com/struts/struts-login

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" 
 "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
    <action-mappings>
         <action path="/switchModule" forward="/success.jsp" />
    </action-mappings>
</struts-config>

2. Deployment Descriptor

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

<init-param>
    <param-name>config/module</param-name>
    <param-value>/WEB-INF/struts-config-module.xml</param-value>
</init-param>

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="http://struts.apache.org/tags-bean" prefix="bean"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Success Page</title>
    </head>
    <body>
        Login Success. Welcome <bean:write name="LoginForm" property="userName"><
    </body>
</html>

4. Modify struts-config.xml

Write the following code in struts-config.xml

<action path="/switchModule" forward="/switch.do?page=/switchModule.do&prefix=/module" />
<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.

3 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.

  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

    “http://localhost:8181/module/switchModule.do”

    instead of
    “http://localhost:8181/StrutsSwitchAction/switchModule.do”

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

    Thank you for noticing my question.

Leave a Reply