Manage Control Flow in Struts Apps

原创 2004年08月25日 17:35:00

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 ""), 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 com.javapro.struts.LogoutAction instance's execute method, and so on.

Three subclasses of Action include LoginAction, LogoutAction, and 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 
  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 ActionMapping and ActionForm objects. For simplicity's sake, I don't use the ActionForm and 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 ActionForm and ActionMapping in Parts 4 and 5.)

Manage Control Flow in Struts Apps (Continued)

In the 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 login.jsp page.

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 Login page.

The ViewSecretAction class's execute method forwards the user to the viewSecret.jsp page.

The application's view consists of three simple JSP pages: login.jsp, mainMenu.jsp, and viewSecret.jsp (see Listings 5, 6, and 7). Note that both mainMenu.jsp and viewSecret.jsp check the user's session object before displaying their contents.

The complete application consists of two configuration files (web.xml and struts-config.xml), three views (login.jsp, mainMenu.jsp, and viewSecret.jsp), and three action classes (LoginAction, LogoutAction, and ViewSecret).

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 
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: ActionServlet and 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 ActionServlet and 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.

About the Author
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, which is licensed and used in projects in major corporations. Contact Budi at


学习TCP协议的流量控制(flow control)小结

TCP 流量控制 窗口机制 窗口扩大因子
  • topasstem8
  • topasstem8
  • 2014年02月19日 22:47
  • 5908

串口流控制(flow control)

  • robertsong2004
  • robertsong2004
  • 2014年08月13日 18:01
  • 5749

Swift笔记(四)之控制流(Control Flow)

Control Flow(控制流)
  • x369201170
  • x369201170
  • 2014年06月12日 17:29
  • 604


简单的说,正规表示法就是处理字符串的方法,他是以行为单位来进行字符串的处理行为, 正规表示法透过一些特殊符号的辅助,可以让使用者轻易的达到『搜寻/删除/取代』某特定字符串的处理程序!       正...
  • qiuwanxiao
  • qiuwanxiao
  • 2015年08月04日 17:43
  • 214


前面,安装在VMWARE中安装了UBUNTN,并且安装了GNURADIO。现在开始学习GRC。 GRC是基于模块的仿真实验工具。类似于MATLAB中的SIMULINK工具。 基本使用方法就是先将需...
  • wwwjjc
  • wwwjjc
  • 2013年01月09日 12:13
  • 954

串口流控制(flow control)

  • u012125696
  • u012125696
  • 2015年05月30日 17:27
  • 2240

串口流控制(flow control)

  • lidandan2016
  • lidandan2016
  • 2017年06月06日 15:05
  • 189

串口流控制(flow control)

  • songshu5555
  • songshu5555
  • 2013年03月25日 10:15
  • 1514


I²C vs SPI Today, at the low end of the communication protocols, we find I²C (for ‘Inter-Integrated...
  • xiahouzuoxin
  • xiahouzuoxin
  • 2013年09月11日 23:09
  • 3209

rocketmq学习笔记 六 流程之存消息

根据前面的 rocketmq学习笔记 六 流程之发消息 rocketmq学习笔记 五 源码之rocketmq-remoting 已经发送消息了,那么broker端怎么处理呢?  ...
  • akfly
  • akfly
  • 2016年12月12日 17:18
  • 1599
您举报文章:Manage Control Flow in Struts Apps