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=”/gotoPageB.do”>Go
to Page B</html:link>
Next, add an
ActionMapping in the Struts Config file as follows:
<action path=”/gotoPageB”
parameter=”/PageB.jsp”
type=”org.apache.struts.actions.ForwardAction” />

The PageA.jsp hyperlink
now points to “/gotoPageB.do”
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”
parameter=”/PageB.jsp”
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
section:
<global-forwards>
<forward name=”pageBForward” path=”/PageB.jsp” />
</global-forwards>

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:

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

<a href=”App1/gotoPageB.do”>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
reason:

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.

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

Comments(5)

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.

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.

Hi,
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