Action Classes in Struts:
Package: org.apache.struts.action
Action.java
Package: org.apache.struts.actions (Total 8)
ForwardAction: only ForwardAction is discussed below in detail.
IncludeAction
DispatchAction
SwitchAction
DownloadAction
LookupDispatchAction
MappingDispatchAction
LocaleDispatchAction
//execute method signature
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)throws Exception {
ActionForward forward =mapping.findForward(forwardPath);
return forward;
}
=================ForwardAction===================
ForwardAction is one of the most frequently used built-in Action classes. The primary reason
behind this is that ForwardAction allows you to follow 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.
NOTE: In Model2 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.
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. 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 you can! First, define an ActionMapping
follows:
<action path=”/gotoPageB” parameter=”/PageB.jsp” type=”org.apache.struts.actions.ForwardAction” />
Next, modify the global forward itself to point to the above ActionMapping.
<global-forwards>
<forward name=”pageBForward” path=”/gotoPageB.do” />
</global-forwards>
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>
Now 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.
Help taken from link: http://www.java-samples.com/showtutorial.php?tutorialid=578
===============END===================
Thursday, March 1, 2012
ForwardAction in Struts
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment