Manage Control Flow in Struts Apps

xiaoxiao2021-03-06  103

Manage Control Flow in Struts AppsYou'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 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 has not already been instantiated ) AND ITS EXECUTE METHOD Performed. if the path equ.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 do not 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 Login 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 / CLASSD DIRECTORY Under The Application Directory.

2. To Compile, you need the struts.jar and the servlet.jar files in your class path. With tomcat, us:

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 does not 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-sellingJava for the Web with Servlets, JSP, and EJB: A Developer's Guide to Scalable Solutions ( New riders) and the developer of the MOPULAR JAVA UPLOAD Bean from

BrainySoftware.com, Which is licensed and used in projects in Major Corporations. Contact Budi At

Budi@brainysoftware.com.

转载请注明原文地址:https://www.9cbs.com/read-123562.html

New Post(0)