[4] Build a beautiful Struts application with actionmapping

xiaoxiao2021-03-06  113

In the fourth part of this series of guides, you can learn how to use ActionMapping to improve your Struts application.

BY Budi Kurniawan

Read the top three parts of this series: Part I: "Your First Struts Application" Part II: "Process Control in Struts Application" Part III: "Optimize your Struts app" with ActionForward "

Churchill once said that he likes to learn new things, but he feels that there is no need to let others teach him. Regardless of whether you like to learn new knowledge, no matter whether someone teaches you self-study, learn Java usually means carefully studying many special classes. This is also true of Struts.

This is the original intention of the fourth part of this series. In this article, I will detail the org.apache.struts.Action.AnPache.Struts.config.Apache.Struts.config.ActionConfig derived. ActionMapping maps a request path to an Action class, which is one of the most commonly used classes in the Struts application. When you study this class in-depth class, you will reuse two login applications created in this series of first, 2nd and 3 to learn how to use ActionMApping to override the application.

Of course, you may not remember any of the previous usemapping instances. This is because the controller servlet actually creates them for you. You only need to configure each anctionsMapping instance created in the struts configuration file (by assigning it to its property). Understanding these properties is important to properly use the ActionMApping class, so I will tell these properties and how to define them.

First, let's recall, the root element of the Struts configuration file is . elements can include an optional element, the same element can include elements. For example, the following is the element in the Login application configuration file in the third part of this series and its sub-elements:

TYPE = "com.javapro.struts.loginaction" />

TYPE = "com.javapro.struts.logoutAction" />

TYPE = "com.javapro.struts.viewsecretation" />

Each in represents an ActionMApping instance created by the controller servlet. A element can contain multiple features, each characteristic is composed of one attribute in the actionMApping instance.

As an example, let's see this element in the Struts configuration file:

TYPE = "com.javapro.struts.loginaction" />

This maps the path "/ login" to the Action class com.javapro.struts.loginaction. In other words, a user request ending with "/ login.do" will be passed to the LoginAction class. However, ActionMapping also has other uses. You (Struts programmer) can get an Action Directive to the ActionMApping instance by assigning a value to its properties. (Some properties are related to Action Forms, I will talk about the fifth part of this series.)

ActionMapping class properties ActionMApping has many properties. First, it inherits some properties from the ActionConfig class - such as Type, Forward, Include, and Unknown. They are independent of Action Forms. The first three attributes are the most important properties of the ActionMapping class. You can only specify one of them, so if a type element has defined a Type property, it can't have a Forward property or an incrude property.

The value of the TYPE property is the name of the fully qualified Java class mapped by the path map. (You have used this attribute in the Struts configuration file in the previous login application.) If you use the Type property, then the controller servlet can call the Action implementation class's Execute method to deliver the appropriate actionMApping instance. Note that the execute method of the org.apache.struts.Action.ActionClass class has the following definition (the first parameter is an ActionMApping instance):

Public ActionForward Execute (ActionMapping Mapping,

Actionform Form, HttpServletRequest Request,

Httpservletresponse response

THROWS IOEXCEPTION, ServletException

The Forward property represents the Context-Relative resource that satisfies the request (by calling the requestDispatcher.forward () instead of instantiation the Action class specified by the Type property. (More information about how to use this property, see the 'Modify Login Application' section later.)

Our value we give the include attribute is the Context-Relative resource path that satisfies the request (by calling requestDispatcher.include () instead of instantiation the Action class specified by the Type property.

Note that elements contained in elements are different from elements that may appear in elements. That is to say, a element can have a Type property, as well as one or more sub-elements, as shown in the element (I immediately tell the element):

TYPE = "com.javapro.struts.loginaction">

Path = "/ mainmenu.jsp" />

PATH = "/ login.jsp" />

We use the attribute to specify this ActionMApping request path. Finally, the unknown path is handled by the unknown property. In an Action element, set this property to True so that this action is the default action of the application. In other words, it processes all other Actions that cannot be processed. In a separate application, only one Action can be defined to be default.

For example, the element is set to TRUE below, making the action a default:

TYPE = "com.javapro.struts.loginaction"

Unknown = "true" />

Element element describes a logical name and a map identified by a context-relative URI path. It has the following properties:

· ClassName This is the name of the fully qualified Java class you want to use. By default, its value is configured to the Struts Controller Servlet as the "Forward" initialization parameter.

ContextRelative In a modular application, if the path property begins with a slash ("/"), and is relative to the entire web application, we will set this property set True. By default, False is

· Name This is the unique identifier for Forward, used to reference it in the application's Action class.

· PATH The Context-Relative path mapped by the resource.

· Redirect is set to TRUE, using sendRedirect () to this resource; or set to false, use RequestDispatcher.Forward () as an alternative.

Use elements under means you don't have to write the path name in your ActionForward object. For example, let's see the code in the Execute method of the LoginAction class in front of the Login application:

Public ActionForward Execute (ActionMapping Mapping,

Actionform Form, HttpServletRequest Request,

Httpservletresponse response

THROWS IOException, servletexception {

String username =

Request.GetParameter ("UserName");

String password =

Request.getParameter ("password");

IF (username! = null && password! = null&&

Username.equals ("john") &&

Password.equals ("123")) {

HttpSession session = request.getations ();

Session.SetaTRibute ("Loggedin", "1");

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

}

Else {

Return ("/ login.jsp");

}

}

Note that the last two returnographs are written by MainMenu.jsp and Login.JSP pages. If any file name changes, you must recompile the LoginAction class. But if you use the element, you can map the MainMenu.jsp page with another name, and map the Login.jsp page with another name. Now, if you need to change the file name, you can change in the configuration file without having to recompilate it. To do this, you need to declare this element in the struts-config.xml file:

TYPE = "com.javapro.struts.loginaction">

Now, MainMenu.jsp is associated with the name of "Success", and login.jsp is in contact with "Failure". You can get an ActionForward instance from an action implementation by calling the Findforward method of the ActionMapping class.

mapping.findforward (name);

For example, to get an ActionForward object containing path "/mainMenu.jsp", we can use the following method:

Mapping.findforward ("Success");

Similarly, you have to get an ActionForward object containing the path "/ login.jsp", you can use:

Writemapping.findforward ("failure");

Next, we should rewrite the Login application with the method.

Modifying the Login application Note what changes have occurred in the Login app created in the third part. First, let's see the Execute method for the ViewSecretAction class in the original application:

Public ActionForward Execute (ActionMapping Mapping,

Actionform Form, HttpServletRequest Request,

Httpservletresponse response

THROWS IOException, servletexception {

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

}

In addition to returning an ActionForward object of "/ViewSecret.jsp", the Execute method does not do anything. We don't need the ViewSecretAction class by using the Forward property of the "Action> element in the struts-config.xml file. To achieve this, we can put the code below:

TYPE = "com.javapro.struts.viewsecretation" />

Replace:

Now, we replace the TYPE attribute with the Forward property. Then, by adding UNKNOWN properties and set it to True, we can make the Login page a default page. In fact, any unknown path (end of .do) will be guided to the LoginAction instance. Handling "/ login" element as follows:

TYPE = "com.javapro.struts.loginaction"

Unknown = "true">

The final modifications made to the LoginAction and the LogoutAction class use the FindForward method for the ActionMapping class to get the appropriate ActionForward object. Declare the mapping in the struts-config.xml file (see List 1). Also, we also take a look at the modified LoginAction class (see List 2) and the modified LogoutAction class (see List 3).

Note that this version uses little Action implementation, because we no longer need ViewSecretAction. Another benefit is: this method avoids writing the path name in the Action implementation class. In addition, the / login path is now default. The result is: Your Struts code ended like a real Struts application. After learning the fifth part (about Action Forms), this program will become better.

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-103109.html

New Post(0)