You have established a deployment descriptor for your first Struts application, and now let's continue learning process control.
BY Budi Kurniawan
After the deployment descriptor is established for your first Struts application (according to the contents described in the first part of the series), you may want to know how the ActionServlet instance controls the program process and takes appropriate action. This is a good problem because in a non-Struts Model 2 application, you usually write a series of IF ... ELSE statements in the controller Servlet string to match a URL. Once the matching URL is found, the controller servlet can execute an action or send a control command. Because you don't write the controller servlet, you may want to know how to manage the process control of your Struts application.
Let's take a detailed story below. In Struts, each URL mode is mapped to a different object called Action Object. Moreover, each Action Object is an instance of a subclass of org.apache.struts.Action.Action class. (The most important way to this class is Execute, I will soon tell.)
Struts uses a profile struts-config.xml (see List 1), which is default it in a web-inferity. This XML file must contain a
The configuration file tells the ActionServlet, if the path is equal to / login (ie, if the URL is ending with "login.do"), then the com.javapro.struts.loginaction object (if it has not been instantiated), and Perform its Execute method. If the path is equal to / logout, then call the Execute method of the COM.JAVAPRO.STRUTS.LogoutAction instance, and so on.
Action contains three subclasses, LoginAction, LogoutAction, and ViewSecretAction (see Listels 2, 3, and 4). In these three Action classes, the Execute method of the Action class has been overloaded. Defined as follows:
Public org.apache.struts.Action.ActionForward
EXECUTE
Org.apache.struts.Action.ActionMapping mapping,
Org.apache.struts.Action.Actionform Form,
Javax.Servlet.http.httpservletRequest Request,
Javax.Servlet.http.httpservletResponse response
Throws Exception
Note that this method returns an ActionForward object, which has four parameters, two of which are an actionMApping and Actionform objects. For the sake of simplicity, I have not used an actionform and an actionmapping parameter in these three Action classes, but returns null values as an alternative. Experts in the Struts may not agree with my approach, but we must start from simple. (I will tell ActionForward in the third part of the series, talked about ActionForm and ActionMapping in the fourth and fifth part.)
In the LoginAction class, the Execute method first gets UserName and password from the request object. If UserName is "john", the password is "123", then we can successfully log in. When you have successfully logged in, it boots the request to the MainMenu.jsp page with a RequestDispatcher object. If the login fails, it boots the request to the Login.jsp page. If the user's session object has not been invalidated, the Execute method in the logoutAction class will fail, and the user returns to the Login page.
The Execute method for the ViewSecretAction class boots the user to the ViewSecret.jsp page.
The view of the application contains three simple JSP pages: login.jsp, mainmenu.jsp and viewsecret.jsp (see Listels 5, 6, and 7). Note that MainMenu.jsp and ViewSecret.jsp check the user's session object before displaying its content.
A complete application contains two profiles (web.xml and struts-config.xml), three views (login.jsp, mainmenu.jsp, and viewsecret.jsp), and three Action, LogoutAction, and ViewSecret.
Execute the following steps to compile .java file:
1. Change the directory to the web-inf / class directory in the application directory.
2. You need the struts.jar and servlet.jar files in the classpath to compile. In Tomcat, you can use the following statement:
Javac-ClassPath
. ../../../../common/lib/servlet.jar;../lib/
Struts.jar COM / JAVAPRO / STRUTS / *. JAVA
3. Restart your web container to run the application, point your browser to http: // Domain / mystrutsapp1 /. (If you run the application on the 8080 port of the local machine, use http: // localhost: 8080 / mystrutsapp1 /.)
Now you have completed a simple, you can run the Struts application. It uses two classes in many classes of Struts: ActionServlet and Action. But it doesn't use the Struts tag library, which is lowered by "writing" and writing code in the JSP file to minimize. In the third part of this series, I will tell the ActionServlet and Action classes in more detail, and introduce the ActionForWard class. I will also modify existing applications, use more Struts classes to help you continue to make progress and become a Struts programmer of intermediate level.
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.