This article is the second in a series of three articles on learning the Struts framework. In the first article of this series, we briefly browsed the Struts framework, the functions of the framework, and the various modules applied in the framework. In this article, I will use Struts 1.0 to demonstrate a simple application for everyone; in the third article, I will introduce how to use Struts tags to access information in the applicationResource file in jsp. In this article, we will explain the application of Struts step by step, opening the mysterious doors of Struts in this way. Through this process, I believe it can also inspire you to apply Struts in application development. If you are not very clear about some terms of Struts, you can refer to the previous article in this series for a general introduction to Struts.
To repeat again, this article requires readers to have knowledge and experience in the following areas: JSP, Servlets, Custom Tag libraries and xml. In addition, in this article, I will also use some other good things from the Jakarta project team, such as Tomcat http://jakarta.apache.org/tomcat/index.Html (a Servlet container that implements the official standards of java Servlet and JSP, popular It is a JSP Web Server) and Ant http://jakarta.apache.org/ant/index.html (a Java-based automatic compilation and publishing tool, this is a good thing).
As a technician who has been using cutting-edge technology to develop many applications, I have always firmly believed that it is crucial to grasp new technologies and understand the logic of technology development. But this is often the quagmire that slows down our learning. Because of this, I plan to use a complete process developed using Struts as a case for our teaching. This example of this process can be said to be "the sparrow is small, but all the five internal organs are included". You can definitely apply this process to the complex and huge projects you have at hand. At least the effect of applying this process in our large projects is good.
Developers who develop complex commercial applications all know that customer needs are always changing, so if there is a standardized development process to follow, when customers put forward new requirements, we can at least clarify what are "unreasonable" The demand is actually reasonable and feasible. Okay, so now I'm going to show you and apply the entire process in my example.
The sample code in this article is part of the StrutsSample application. The complete code including build.xml can be downloaded here http://www.onjava.com/onjava/2001/10/31/examples/StrutsPartII.jar.
Struts development process
It can be seen from the version number released by Struts that Struts is a new thing. It consists of several parts. It will be better if you know when to develop the appropriate parts. Take advantage of our development time. From several Struts applications I have developed, I have roughly summarized the following more effective development steps:
1. Clarify the application requirements;
2. From the perspective of user input and data acquisition, clarify and design Each user interface;
3. Determine the entry path of the user interface;
4. Determine the action mapping table (ActionMapping) from the application logic information;
5. Develop the classes and applications used by the designed user interface Function;
6. Develop ActionForm and corresponding data verification method from the data information in the user interface;
7. The corresponding Action will be called or transferred to the corresponding JSP page in ActionMapping. In this step, we develop these first Action;
8. Develop business application logic, which is the corresponding JavaBean, EJB or other stuff;
9. Develop the system workflow defined by ActionMapping to complete the corresponding JSP page;
10. Complete the system configuration file: struts -config.xml and web.xml;
11, compile/test/release.
Clear application requirements
The first step in developing any application system is to collect user demand information. No matter how reasonable a user logic may seem at first, there is always a chance that you will discover during development that it is much more difficult than it seems. Therefore, it is recommended to draw up a clear list of user requirements, not only for development purposes, but also to analyze user needs through this list to determine where more effort may be needed.
In our StrutsSample project, the application requirements are:
As a complete example showing the Struts framework application, the function completed by this example is user login. The purpose is only to clarify the application of Struts. This example will not involve security, database, EJB development and other related technologies that may be applied in general complex application systems.
Design user interface
This application includes the following three user interfaces:
1) Login interface for user name and password input;
2) Welcome interface when the logged-in user is a legitimate user;
3) Error prompt interface when login fails.
Determine the entry path to the user interface
1) The login interface is used as the default page of this application;
2) The welcome interface can only be entered after successful login;
3) Any page where an error may occur can enter the error prompt interface;
The ActionMapping ActionMap is determined by the application logic information. This article is the second in a series of three articles on learning the Struts framework. In the first article of this series, we briefly browsed the Struts framework, the functions of the framework, and the various modules applied in the framework. In this article, I will use Struts 1.0 to demonstrate building a simple application for everyone; in the third article, I will introduce how to use Struts tags to access information in the ApplicationResource file in JSP. In this article, we will explain the application of Struts step by step, opening the mysterious doors of Struts in this way. Through this process, I believe it can also inspire you to apply Struts in application development. If you are not very clear about some terms of Struts, you can refer to the previous article in this series for a general introduction to Struts.
To repeat again, this article requires readers to have knowledge and experience in the following areas: JSP, Servlets, Custom Tag libraries and XML. In addition, in this article, I will also use some other good things from the Jakarta project team, such as Tomcat http://jakarta.apache.org/tomcat/index.html (Servlet that implements Java Servlet and JSP official standards can be used as the entire The application determines the "road map" and defines the ActionMapping in the configuration file struts-config. In the process, the information required for ActionMapping is gradually determined. The process of developing the code is the process of improving struts-config.xml step by step starting from the draft. When the Action class processes the user request, the forward it returns is in ActionMapping. Defined one. There are many possibilities for the forward returned by an Action, although an Action generally only defines several related forwards. If there are multiple Actions that may return the same forward, then it can be defined as. Global forward. This is similar to the global variables in the header file in C. If in the struts-config.xml description information, a certain forward is not defined in the current Action description but is defined globally, then this The global one will work. Similarly, the forward currently defined in an Action will override the global definition. In the simple example we gave, we defined the global forward - "error". When an Action returns the forward "error". "This mapping, then the Errorpage.jsp page will be displayed to the user, although the current Action does not define it. We continue to develop, the project is getting better and better, and the project-related configuration files will become more and more specific. In the following example In, we will take the struts-confug.xml file used in StrutsSample as an example to learn the definition of global forward and related mapping in an Action. An Action named "login" is defined below, which is com.oreilly.actions. An instance of .LoginAction. When the Action processes the user's successful login, it will return a forward named "sUCcess", and the user will see the Welcome.jsp page. If the login fails, the Action will return the corresponding forward to display Login.jsp again. To the user, if other errors occur during processing, the Action will return the globally defined forward-"error", and the user will also see the error page Errorpage.jsp.
name="loginForm"
scope="request"
input=" /Login.jsp"> Login.jsp"/>
In the previous article, we once said that struts-config.xml is the Controller of the MVC mode. When determining the configuration information in struts-config.xml, you should spend more time and energy on it to ensure that every .php.cn)!