Manage Control Flow in Struts Apps
You've finished the deployment descriptor for your first Struts app. Now learn to forward control.
by Budi Kurniawan
Posted September 24, 2002
After finishing the deployment descriptor for your first Struts application, as you did in Part 1 of this series, you might wonder how the
ActionServlet instance knows where to forward control or what action to take. That's a good question, because in a non-Struts Model 2 application, you normally write a series of if…else statements in the controller servlet to match a URL against a number of predefined strings. Once a match is found, the controller servlet can perform an action or forward control. And because you don't write the controller servlet yourself, you might wonder how you'll manage the control flow of your Struts application.
Here's how it works. In Struts, each URL pattern is mapped to a different object called an action object. In turn, each action object is an instance of a subclass of the
org.apache.struts.action.Action class. (The most important method of this class is
execute, which I'll discuss shortly.)
Struts uses a configuration file,
struts-config.xml (see Listing 1), which resides by default under the WEB-INF directory. This XML file must contain an <action-mappings> tag that maps a path with an action object.
The configuration file tells the
ActionServlet that if the path equals
/login (that is, if the URL ends with "login.do"), the
com.javapro.struts.LoginAction object must be instantiated (if it hasn't already been instantiated) and its
execute method performed. If the path equals
/logout, invoke the
execute method, and so on.
Three subclasses of
ViewSecretAction (see Listings 2, 3, and 4). In these three action classes, the
Action class's execute method has been overridden. Here is the signature:
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 the method returns an
ActionForward object and takes four arguments, two of which are
ActionForm objects. For simplicity's sake, I don't use the
ActionMapping arguments in the three action classes, returning
null instead. A Struts expert would probably disapprove, but everyone has to start somewhere. (I'll discuss
ActionForward in Part 3 of this series, and
ActionMapping in Parts 4 and 5.)
Manage Control Flow in Struts Apps (Continued)
LoginAction class, the execute method starts by getting the
userName and password parameter from the request object. A successful login happens if the
userName value is "john" and the password is "123". Upon a successful login, it forwards the request to the
mainMenu.jsp page using a
RequestDispatcher object. If login fails, it forwards the request to the
The execute method in the
LogoutAction class invalidates the user's session object, if it hasn't been already been invalidated, and forwards the user to the
ViewSecretAction class's execute method forwards the user to the
The application's view consists of three simple JSP pages:
viewSecret.jsp (see Listings 5, 6, and 7). Note that both
viewSecret.jsp check the user's session object before displaying their contents.
The complete application consists of two configuration files (
struts-config.xml), three views (
viewSecret.jsp), and three action classes (
To compile the .java files, perform these steps:
1. Change directory to the WEB-INF/classes directory under the application directory.
2. To compile, you need the
struts.jar and the
servlet.jar files in your class path. With Tomcat, use:
javac -classpath ../../../../common/lib/servlet.jar;../lib/ struts.jar com/javapro/struts/*.java
3. To run the application, restart your Web container and point your browser to http://domain/myStrutsApp1/. (If you are running the application on a local machine on port 8080, use http://localhost:8080/myStrutsApp1/.)
So there you have it: a simple, working Struts application. It uses two of the many classes in Struts:
Action. But it doesn't use the Struts tag libraries and minimizes the number of files by hard-coding some values and writing code in the JSP files. In Part 3 of this article, I'll explain the
Action classes in more detail and introduce the
ActionForward class. I'll also modify the current application and use more Struts classes, to help you advance toward an intermediate level as a Struts programmer.
Budi Kurniawan is an IT consultant specializing in Internet and object-oriented programming and has taught both Java and Microsoft technologies. He is the author of the best-selling Java for the Web with Servlets, JSP, and EJB: A Developer's Guide to Scalable Solutions (New Riders) and the developer of the most popular Java Upload Bean from BrainySoftware.com, which is licensed and used in projects in major corporations. Contact Budi at email@example.com.