Struts ForwardAction

Struts ForwardAction forwards the current request to the specified resource. ForwardAction allows you to adhere to MVC paradigm when designing JSP navigation.

Instead of direct giving jsp link violate the MVC design pattern, so this action is usable in this scenario. In this case it is not required to create action class, we will give directly ForwardAction class in configuration.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.10</version>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-taglib</artifactId>
      <version>1.3.10</version>
    </dependency>
     
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-extras</artifactId>
      <version>1.3.10</version>
    </dependency>
</dependencies>

1. Create ForwardAction

Create the Action Mapping in struts-config.xml

<action-mappings>
        <action path="/helloWorld" type="org.apache.struts.actions.ForwardAction" parameter="/helloWorld.jsp"/>
</action-mappings>

For each link, create separate action-mapping. The three attributes shown above are mandatory in a ForwardAction. The “type” attribute is always org.apache.struts.actions.ForwardAction instead of a custom action. The “path” attribute identifies the URL path. The “parameter” attribute in the above definition is the URL for the next JSP.

2. Create JSP Page

Using LinkTag’s action attribute

index.jsp

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:link action="/helloWorld">Click here to access Hello World Page
</html:link>
 
or 
 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:link action="/helloWorld.do">
           Click here to access Hello World Page
</html:link>

When you use the action attribute instead of the page attribute in , you need not specify the “.do” explicitly.

Using LinkTag’s forward attribute

index.jsp

We can use forward attribute instead of action attribute.

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:link forward="helloWorld">Click here to access Hello World Page
</html:link>

When forward attribute is used, we have to create global-forward entry in struts-config.xml as follows:

<global-forwards>
        <forward name="helloWorld" path="/helloWorld.do"/>
</global-forwards> 

Now create helloWorld.jsp file that display Hello World!!! message.

HelloWorld.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
</head>
<body>
<h1>Hello World!!!
</h1>
</body>
</html> 

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

Figure 4.1 Figure 4.1

You can download the source code of this example here.

Comments are closed.