ForwardAction in Struts

By: Daniel Malcolm Viewed: 153468 times  Printer Friendly Format    

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” />


Most Viewed Articles (in Struts )

Latest Articles (in Struts)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. very well written.i searched whole day for this s
View Tutorial          By: Bishal Bhatta at 2010-06-26 00:22:57

2. This tutorial is really Good and enhance my desire
View Tutorial          By: Yasin at 2011-07-09 09:54:13

3. This tutorial is really Good and enhance my desire
View Tutorial          By: Yasin at 2011-07-09 10:04:57

4. very nice and informative
View Tutorial          By: Ravi at 2011-09-15 19:31:57

5. I want to know why struts framework provide Forwar
View Tutorial          By: bijna at 2012-07-18 21:59:34

6. really a nice tutorial for beginners...Thanks a lo
View Tutorial          By: ps at 2013-02-26 09:28:53

7. Using ForwardAction for Integration is Good, Which
View Tutorial          By: Ramesh Ram at 2013-06-05 17:57:25

8. Hi Daniel,

Please suggest me, inste

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

9. Hi,
I generally dont write comment on artic

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

10. iwufifuyiki
View Tutorial          By: iwufifuyiki at 2017-08-02 11:22:55

11. okuibipiwiji
View Tutorial          By: okuibipiwiji at 2017-08-02 12:52:04

12. uviixocez
View Tutorial          By: uviixocez at 2017-08-02 13:06:16

13. spiroyigagesi
View Tutorial          By: spiroyigagesi at 2017-08-02 16:13:50

14. otelayog
View Tutorial          By: otelayog at 2017-08-02 20:14:28

15. ijupihuqa
View Tutorial          By: ijupihuqa at 2017-08-02 20:29:21

16. ijmokanu
View Tutorial          By: ijmokanu at 2017-08-02 23:43:20

17. uxovavexogihe
View Tutorial          By: uxovavexogihe at 2017-08-03 04:51:18

18. ubusujeuquccu
View Tutorial          By: ubusujeuquccu at 2017-08-03 05:07:28

19. onucuteyom
View Tutorial          By: onucuteyom at 2017-08-03 07:46:55

20. imamewi
View Tutorial          By: imamewi at 2017-08-03 08:26:56