[3] Optimize your Struts app with ActionForward

xiaoxiao2021-03-06  108

The Struts's ActionForward class allows you to complete your tasks easier, faster and more directly.

BY Budi Kurniawan

Read the first three parts of this series: Part 1: "Your First Struts Application" Part II: "Process Control in Struts Application"

It also contains complexity, we have created a simple Struts login app while researching a simple Struts login app while researching a simple Struts login application, and has avoided many of this complexity. Because the two articles are introduced to beginners' Struts, we have not fully utilized the Struts class library in the app. But now, you have been preparing to learn more things. Let's first take a look at how the real Struts programmer uses the ActionForward class, which represents a destination resource oriented by the control instruction.

From the inside of a servlet, you can guide the control process to a destination resource by using the Javax.Servlet.RequestDispatcher class. In the Action class of the Login application, the code form is as follows:

RequestDispatcher rd =

Request.getRequestDispatcher (Destination);

Rd.Forward (Request, Response);

Where Destination is the path to a destination resource.

But in a typical Struts application, you can use the ActionForward class as an alternative. The advantage of using this class is that you no longer need to create a RequestDispatcher object and call it Forward method.

You can use the ActionForWard class for an Execute method of an Action class. Note that one of the overloaded Execute methods has the following definition, it returns an ActionForward object:

Public ActionForward Execute

ActionMapping mapping,

Actionform Form, HttpServletRequest Request,

Httpservletresponse response

Throws Exception

Because we have not yet tailored the ActionForward class, all Action classes of all Action classes in the first part of this series returns null values. Now, in an Execute method of an Action class, you can use the ActionForward class to replace the following RequestDispatcher object instance:

RequestDispatcher rd =

Request.getRequestDispatcher (Destination);

Rd.Forward (Request, Response);

The new code becomes:

Return (New ActionForward (Destination));

Build an ActionForward object ActionForward class provides the following five constructors:

Public anctionforward ()

Public ActionForward (String Path)

Public ActionForward (String Path, Boolean

REDIRECT)

Public ActionForward (String Name, String Path,

Boolean Redirect)

Public ActionForward (String Name, String Path,

Boolean Redirect, Boolean ContextRelative)

Although these constructors do not need, we should pay attention to the following. In these constructors, the second may be most common. The path parameter in the latter four constructors is represented by the path to the destination resource. The Redirect Boolean value in the latter three constructors indicates whether a redirect is executed. (By default, this value is set to false because the redirect is slower than Forward.) Finally, the CONTEXTRELATIVE Boolean value in the fifth constructor indicates whether the path should be context-relative, not Module-Relative. Similarly, an ActionForward instance can also have a logical name that you can use this name to find an instance related to a special ActionMapping object. (See the fourth part of this Series about ActionMapping.)

Method ActionForward classes for learning ActionForward defines three protection fields --Name, Path and Redirect - they constitute three properties of the ActionForward. The ActionForWard class provides Getter and Setter methods to read values ​​from these fields to assign these fields. These methods are not required, defined as follows:

Public Boolean getContextRelative ()

Public void setContextRelative (Boolean

ContextRelative)

Public string getName ()

Public void setname (String name)

Public String getPath ()

Public void setpath (String Path)

Public Boolean getRedirect ()

Public void setRedirect (Boolean Redirect)

In addition, the ActionForward class is overloaded with the toString method and returns:

"ActionForward [" Name "]"

Where Name is a name field.

Finally, there is a Freeze method that fixes a component configuration.

Using the Login application again to fully understand the ActionForward class, we need to use the Login application built in the first part of this series and the second part. You can download the full application and rename it as mystrutsapp2. Its web.xml and struts-config.xml files are the same as files in MyStrutsApp1, and the JSP page has not changed. Only the Action class is different from before (see List 1).

Note The following line of code is new:

Return (New ActionForward ("/ mainmenu.jsp"));

It replaces the following code, now they are commented out:

RequestDispatcher rd =

Request.getRequestDispatcher ("/ mainmentu.jsp");

Rd.Forward (Request, Response);

Similarly, the following code has also been rewritten:

// RequestDispatcher rd =

Request.getRequestDispatcher

"/login.jsp");

// rd.forward (request, response);

The new code becomes:

Return ("/ login.jsp");

The ViewSecretAction class ViewSecretAction has also become better (see List 2). The last three lines of code of the execute method are now handled by one line, returned ("/ViewSecret.jsp")): // RequestDispatcher rd =

Request.getRequestDispatcher

"/viewsecret.jsp");

// rd.forward (request, response);

// Return NULL;

Next, let's re-view the LogoutAction class (see List 3). Note The following code in the Execute method has been replaced:

// RequestDispatcher rd =

Request.getRequestDispatcher

"/login.jsp");

// rd.forward (request, response);

// Return NULL;

You just need to replace it with the following line of code.

Return ("/ login.jsp");

ActionForward is a very useful, multi-functional class, which makes you more simple, faster, and more directly, this may be a very popular reason. In the fourth part of this series, you can understand another important class org.apache.struts.action.actionmapping, which makes your code more efficient, more beautiful.

About the Author:

Budi Kurniawan is an IT consultant that he specializes in Internet and object-oriented programming, teaches Java and Microsoft technology. He is very good

Java for the Web with servlets, JSP, And EJB; A Developer's Guide To Scalable Solutions (New Riders) Author, he also developed the most popular Java Upload Bean, you can

BrainySoftware.com gets it, many important companies have licensed and used it in the project. Budi contact information is

Budi@brainysoftware.com.

转载请注明原文地址:https://www.9cbs.com/read-103110.html

New Post(0)