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
TYPE = "com.javapro.struts.loginaction" /> TYPE = "com.javapro.struts.logoutAction" /> TYPE = "com.javapro.struts.viewsecretation" /> action-mappings> Struts-Config> Each As an example, let's see this TYPE = "com.javapro.struts.loginaction" /> This 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 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 TYPE = "com.javapro.struts.loginaction"> Path = "/ mainmenu.jsp" /> PATH = "/ login.jsp" /> action> We use the For example, the TYPE = "com.javapro.struts.loginaction" Unknown = "true" /> · 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 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 TYPE = "com.javapro.struts.loginaction"> action> 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 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 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" 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.