ForwardAction in Struts

By: Daniel Malcolm Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

ForwardAction is the one of the most frequently used built-in Action classes. The primary reason behind this is that ForwardAction allows you to adhere to MVC paradigm when designing JSP navigation. Most of the times you will perform some processing when you navigate from one page to another. In Struts, this processing is encapsulated in the Action instances. There are times however when all you want to do is navigate from one page to another without performing any processing. You would be tempted to add a hyperlink on the first page for direct navigation to the second. Watch out! In Model 2 paradigm, a straight JSP invocation from another JSP is discouraged, although not prohibited. For instance, suppose you want to go from PageA.jsp to PageB.jsp in your Struts application. The easy way of achieving this is to add a hyperlink in PageA.jsp as follows:

<a href="PageB.jsp">Go to Page B</a>

or even better, as follows:

<html:link page=”/PageB.jsp”>Go to Page B</html:link>

However this violates the MVC spirit by directly accessing the JSP. In Model 2 applications, it is the responsibility of the Controller to select and dispatch to the next view. In Struts, ActionServlet and Action classes together form the controller. They are supposed to select and dispatch to the next view. Moreover the ActionServlet is responsible for intercepting your request and providing appropriate attributes such as Message Resource Bundles. If you bypass this step, then the behavior of the Struts tags may become unpredictable.

MVC compliant usage of LinkTag

Struts provides a built-in Action class called ForwardAction to address this issue. With ForwardAction, the Struts Controller is still in the loop while navigating from PageA to PageB. There are two steps involved in using the ForwardAction. They are:

 First, declare the PageA hyperlink that takes you to PageB as follows:
<html:link page=”/”>Go to Page B</html:link>
 Next, add an ActionMapping in the Struts Config file as follows:
<action path=”/gotoPageB”
type=”org.apache.struts.actions.ForwardAction” />

The PageA.jsp hyperlink now points to “/” instead of “PageB.jsp”. This ensures that the controller is still in the loop. 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 of yours. The path attribute identifies the URL path, as any other ActionMapping. The parameter attribute in the above definition is the URL for the next JSP.

In the above ActionMapping you might have noticed there is no ActionForm. The Struts Config file DTD specifies that the Form bean is optional in an ActionMapping. Logically speaking ActionForm makes sense only where is data to be collected from the HTML request. In situations like this where there is no HTML data involved in the navigation, there is no need for ActionForm.

Using LinkTag’s action attribute

The LinkTag (<html:link>) has several variations. It can be used in a variety of ways in conjunction with ForwardAction. You just saw one usage of the LinkTag. A second way of using the this tag is as follows:

 First, declare the PageA hyperlink that takes you to PageB as follows:
<html:link action=”gotoPageB”>Go to Page B</html:link>
 Next, add the ActionMapping for /gotoPageB in the Struts Config file
same way as before:
<action path=”/gotoPageB”
type=”org.apache.struts.actions.ForwardAction” />

When you use the action attribute instead of the page attribute in <html:link>, you need not specify the “.do” explicitly.

Using LinkTag’s forward attribute

There is yet another way to use <html:link>. In this approach you use the forward attribute of the <html:link> tag instead of the action. There are two steps involved in this approach.

 First, declare the PageA hyperlink that takes you to PageB as follows:
<html:link forward=”pageBForward”>Go to Page B</html:link>
 Add a Global Forward for “pageBForward” as follows in the globalforwards
<forward name=”pageBForward” path=”/PageB.jsp” />

When used in this manner, the <html:link> gets transformed into the following HTML Link.

<a href=”App1/PageB.jsp”>Go to Page B</a>

Oops, that doesn’t seem right. The HTML Link is now displaying the actual JSP name directly in the browser. Ideally you would love to hide the JSP name from the user. And with a slight twist you can! First, define an ActionMapping as follows:

<action path=”/gotoPageB”
type=”org.apache.struts.actions.ForwardAction” />

Next, modify the global forward itself to point to the above ActionMapping.

<forward name=”pageBForward” path=”/” />

When used in this manner, the <html:link> gets transformed into the following HTML Link.

<a href=”App1/”>Go to Page B</a>

There you go! The generated HTML is not displaying the JSP name anymore. From a design perspective this seems to be the best way of using the <html:link> tag since the link is completely decoupled from the associated ActionMapping, thanks to the global-forward. The <html:link> points to the global-forward and the global-forward points to the ForwardAction. The extra level of indirection, although looks confusing in the beginning, is a good design decision due to the following

As is true with any application, requirements change and it might just become necessary to do some processing during the navigation from PageA to PageB. A conversion from ForwardAction to a custom Action will be easier to manage with the extra level of indirection.

Using ForwardAction for Integration

In general, the ForwardAction’s parameter attribute specifies the resource to be forwarded to. It can be the physical page like PageB.jsp or it can be a URL pattern handled by another controller, maybe somewhere outside Struts. For instance, consider the following ForwardAction.

<action path=”/gotoPageB”
type=”org.apache.struts.actions.ForwardAction” />

In the snippet above, the value of the parameter is not a physical page. It is a logical resource that might be mapped to another Servlet totally outside the control of Struts. Yet from PageA’s perspective, you are still dealing with a Struts URL. This is the second use of ForwardAction. You can integrate Struts applications transparently with already existing non-Struts applications. 

NOTE: Even with the ForwardAction, you cannot prevent a nosy user from accessing the JSP directly. See the section Protecting JSPs from direct access for techniques to protect your JSPs from direct access.

ForwardAction Hands-on
Think of index.jsp as PageA and CustomerDetails.jsp as PageB. The <html:link> in index.jsp will look as follows: <html:link forward=”CustomerDetailsPage”>Customer Form</a> The following Global Forward and ForwardAction are added to the Struts Config file.

<forward name="CustomerDetailsPage"
path="/" />

<action path=”/gotoCustomerDetails”
type=”org.apache.struts.actions.ForwardAction” />


Struts Home | All Struts Tutorials | Latest Struts Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

This tutorial is really Good and enhance my desire to know more about struts navigation.Thank a lot Daniel Malcolm. But isn't it boring and irritating to follow this process for a project where lots of navigation is required in the menu for each of the navigation. If u know any solution for that then it will be very helpful for me to apply into my running project.
In addition to that I also like to know ... how i can perform some tasks like store some values in the formbean from session and database so that i can place them in the text field and combo box when the page loaded.
Thank u very much once again for such a nice tutorial.

Be Well.
God Bless u.

View Tutorial          By: Yasin at 2011-07-09 10:04:57
2. View Comment

really a nice tutorial for beginners...Thanks a lot..

View Tutorial          By: ps at 2013-02-26 09:28:53
3. View Comment

Using ForwardAction for Integration is Good, Which Provide practical perspective and in depth knowledge about struts.

Thanks to Daniel

View Tutorial          By: Ramesh Ram at 2013-06-05 17:57:25
4. View Comment

Hi Daniel,

Please suggest me, instead of forwarding the control to another jsp page inside the same application, if we want to forward the control to another url running on a different server(or same server), what is the procedure to do that ... ? Please suggest the sample code if possible.

View Tutorial          By: Pradeep at 2013-12-05 07:35:32
5. View Comment

I generally dont write comment on articles mentioned on the site. By going through forwardaction and various ways of doing bit and explained through example was the really enlighening experience.Though i read ForwardAction before but really wasnt aware of some many ways. Thanks a lot for writing this for all. Your article compelled introvert like me to congratulate for your good woork

View Tutorial          By: Ankur Goyal at 2013-12-20 09:21:34

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

More Tutorials in Struts
Configuring JDBC DataSources in Struts
Struts Classes
FAQ: Why was reload removed from Struts (since 1.1)?
FAQ: Why are my checkboxes not being set from ON to OFF?
Using JavaScript to submit a form in Struts
How to prepopulate a form in Struts
Simple example of using the requiredif Validator rule in Struts
Chaining actions in Struts
When is the best time to validate input in Struts
What is a Plug-in and how to use Java plug-ins with Struts?
7 Best Practices of Struts
Origin and Architecture of Struts
ActionErrors and ActionError in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts

More Latest News
Most Viewed Articles (in Struts )
ForwardAction in Struts
Configuring JDBC DataSources in Struts
Struts 1 vs Struts 2
MVC Architecture (Model 2 Architecture)
Installing and configuring Tomcat and Struts
Struts and Tiles - Steps to use Struts and Tiles
Creating Struts Modules
Using Multibox in Struts
Use ImageButtonBean for Image based Form Submits in Struts
Tutorial on Struts Configuration File - struts-config.xml in Struts - from the book: Struts Survival Guide. Basics to Best Practices
Handling multiple buttons in HTML Form in Struts
Handling Duplicate Form Submissions in Struts
Struts-GUI and Struts Console
Origin and Architecture of Struts
Using JavaScript to submit a form in Struts
Most Emailed Articles (in Struts)
What is a Model View Controller (MVC) Model?
What is Struts? Which Version of Struts to use?
Is Struts the most popular web application framework for Java?
History of Struts
WebWork 2 is now Apache Struts 2
Configuring JDBC DataSources in Struts
Struts Classes
Downloading and installing Struts
The directories and files of a Struts application
Editing web.xml in a Struts Application
Editing struts-config.xml in a Struts Application
Creating the first application using Struts 2
Struts 1 vs Struts 2
Model 1 Architecture
MVC Architecture (Model 2 Architecture)