Manage Control Flow in Struts Apps
来源:互联网 发布:安全知识网络竞赛入口 编辑:程序博客网 时间:2024/05/17 09:44
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 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 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 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 ../../../../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: 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.
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 budi@brainysoftware.com.
- Manage Control Flow in Struts Apps
- Flow control in ksh
- Control Flow in Node
- Control Flow in JavaScript
- 3. Flow control in VBScript
- Flow Control in TCP 3
- Some tips about Control Flow in Python
- Flow Control in TCP(tcp的流量控制实现)
- Minidriver Flow of Control
- Item 47. Control Flow
- Exceptions and control flow
- Program flow control (Notes)
- Control Flow Functions!
- Producer Flow Control
- LSL-- Flow Control
- Reliability and Flow Control
- Chapter 4 - Flow Control
- Python en:Control Flow
- 试用blog
- 周星星的经典台词在生活中的应用
- 关于C++模板和重载的小问题
- 对项目目标的一点想法
- DataGrid设计
- Manage Control Flow in Struts Apps
- “符号化”的效用和缺失
- Flash MX 2004 7.2 Update 简体中文版提供下载
- 免费的开放源代码数据库 PostgreSQL 8.0 试用版发布!
- 如何在视图状态中保存自定义的对象
- Spring-Validator 说明文档
- 开始处
- 收集一些常用的正则表达式
- SQL Server 安全检查列表