Examples of the Struts Enterprise Web Development Framework (2)

xiaoxiao2021-03-06  115

Preparation Phase

A typical editing session requires an action processor to prepare View, which is a JSP page as a view, and requires a second action processor to store updated View. Of course, there will be a page that is a page belonging to a view after the storage operation (see Table 1) is displayed, for example, a "data has been updated, click here".

Table 1: Parts to be used based on Form bean

Component Description CatalogformForm BealeditcategoryAction Preparation Phase Editcategory.jsp Edit SavecateGoryAction Storage Phase EditDone.jsp Confirm Data Save EditFailed.jsp "Data No Save" error

The following code snippet shows how to configure the preparation stage in the struts-config.xml file:

Scope = "request"

Name = "catform"

TYPE = "com.strutsdemo.idection"

unknown = "false"

Validate = "false">

PATH = "/ editcategory.jsp" />

In the preparation phase, the container attempts to find the specified form bean from session or request, because "Name = ..." is specified in the action. ActionServlet is on the struts-config.xml file

The area is looking for an alias of Form Bean, and uses Form Beans to find the corresponding Java class. If the user's request with parameters, its name matches the parameter of the FORM BEAN attribute name will be set to the attribute value. The Struts extends the meaning of the "Property Name", making the properties of the access to the Form Bean embedded objects possible. The examples of this article also use this advantage of Struts.

After preparing the Form Bean, the ActionServlet then calls the action process () method, the Form Bean is incorporated into the Process () method. Here, our final adjustment of the Form Bean, calls the business method, delegates the editcategory as a view to generate a HTML page based on the form bean. This page is passed to the client, and then the "performance" phase will be entered.

Performance phase

This phase of the user edits the form and submits. If the server is applied to the content of the server, there is a problem with the content submitted, it displays the form to the user, plus the appropriate prompt information; repeat the process until the user submits a legal form, or cancels the form processing process. The suspension of the editing process may be due to the user's jump to other pages, or start a cancel action (for example, a button defined by the HTML: Cancel tag). Although in theory, the VIEW's verification and re-display operation should be a stage of expression, but in the Struts application, this part of the function is most convenient in the storage phase.

Storage phase

The preparation phase creates actions CATForm with the "name =" attribute defined, and the storage phase is added to the additional attribute, namely "validate = 'true'" and "input =" attributes.

Scope = "request"

Name = "CATFORM" type = "com.strutsdemo.savecategoryaction"

unknown = "false"

INPUT = "/ editcategory1.jsp"

Validate = "True">

Path = "/ categoryupdated.jsp" />

After setting the "validate = 'true'" property option, the server will add a processing step. Re-use the data from view to the FORM bean, or when updating from bean, the FORM BEAN's validate () method is called. The validate () method performs the necessary legitimacy verification operations. If there is an error in the user's input data, the validate () method creates one or more ActionError objects. These ActionError objects contain the name of the error message ID and form input field. These ActionError objects are collected and sorted into an ActionerRors object, and then the ActionerRors object returns by the validate () method. If the data entered by the user does not include an error, Validate () returns NULL.

Since the "Input =" property is specified, the action will be ignored once an error occurs, and the "INPUT =" specified by the VIEW is displayed. This view contains both Form Beans, also contains the currently incorrect object collection. Generally, this input page is the JSP page that originally performs editing functions.

Most Struts HTML tags have corresponding HTML tags, but Struts has an HTML no mark, namely

mark. To abort the form editing process, the user can manually enter the URL, or you can click on the link not point to the storage action processor. Therefore

The "Cancel" button defined by the tag is not the only way to cancel the editing operation.

Suppose the validate () method does not find any errors, and the user does not click the "Cancel" button, the process () method of the storage action will be called. In this process () method, we call the Form Bean's Save () method to write the data into a persistent storage device, and then display "Successful" Successful "or" Storage Action Failure "is displayed based on the write operation. VIEW.

============================================================================================================================================================================================================= ==========================================================================================================================================================

Construct and run Struts application

To construct and run the sample application of this article, you must learn how to use Jakarta's Ant tools. If you still don't understand Ant, it is time to learn it! Hurry to the website to download Ant, usually unwind to compress, set up Ant_home environment variables, and then add Ant to the execution path. The build.xml of this article needs to be customized slightly, modify the configuration of the local Tomcat location, enabling it to find the class necessary to compile under Tomcat. In addition, you have to have a struts of JAR. You can download the latest version.

The struts-config.xml file is a configuration file that bonds the Struts application. In a web application after deployment, struts-config.xml is in a web-infirectory. You should modify the data source configuration so that it meets your current DBMS environment. The data model and SQL schema files are written for PostgreSQL DBMS in the DBMS directory of the download package.

In the example, SRC / COM / STRUTSDEMO / SHOWCATEGORIESATION. JAVA is a simple dispenser. ActionForward () is a request allocation method that is called from an ActionServlet. This method can complete the main work, such as analysis request parameters, perform calculations, and JavaBean constructing VIEW. In addition, the method also determines what page to display according to the result of the processing: It may be one of the plurality of pages of the preset, or an error message page.

ActionForward () request allocation process

Of course, the most complex processing procedure is related to the form. The request allocation process for the ActionForward method is:

1. ActionServlet, decoding the request. Since the Form Bean is specified for the action, the ActionServlet processes the Form Bean (see "How to use the ActionServlet how to use Form bean"). Then, the request is passed to the EditCateGoryAction.

2. EditcategoryAction; Prepare the process of processing, or from the database into existing data, or create a new data item. The action processor uses mapping.forward to pass the control to editcategory.jsp.

3. DITCATEGORY.JSP shows the Form Bean, allowing users to edit data. After the user submits the data, control to the ActionServlet.

4. ActionServlet, decode the request. This time, the Form Bean will initialize the data of View because it is a Struts JSP table single page. Due to the Form Bean, "Validate = 'True'" is specified in Struts-Config.xml, the validate () method of the FORM BEAN name "CATFORM" is called. If the data submitted by the user cannot be verified by legality, the control is turned to editcategory1.jsp.

5. Editcategory1.jsp, it is just a version after editcategory.jsp. If necessary, the original editing page and the editing page with the error prompt can use the same view. The Struts JSP tag can help us do this easily. This page is submitted to /savecategory.do. In this way, the user has a circle between these pages until he jumps to a page that is not related to editing operation, or the data he submits is verified by legality.

6. If the Form Bean legality is verified, the ActionServlet passes the request (including Form Bean) to SavecategoryAction. In this example, "Save" may mean create an operation, which may also mean the update operation, which is determined by the option provided by the URL. The operation of writing data is done by calling the Store () method of the Form Bean. Note: The actual application should use some type of transaction management mechanism (or use EJB because EJB has built-in transaction management mechanism) to avoid concurrent access. How to use Form Bean using formalvlet

The ActionServlet process involving the Form Bean contains six steps:

1. Find or create form bean;

2. According to the corresponding data incorporated from the HTTP request, update the various properties of the FORM BEAN;

3. Check if the user clicks the "Cancel" button. If it is skipped, steps 4 and 5;

4. Verify the legality of Form Bean data;

5. If the data is not verified by legality, the VIEW specified in the "Input =" parameter is sent;

6. Otherwise, pass the Form Bean to the action processor.

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

New Post(0)