JAKARTA STRUTS application practice

zhaozj2021-02-16  107

In this article, we will explain the Struts' application in this article. Open the Struts a mysterious door in this form. Through this process, I believe that you can use the inspiration of how you apply Struts in application development. If you are not very clear about Struts, you can refer to this series of articles on Struts.

Repeat again, this article requires readers to have the following aspects of knowledge and experience: JSP, servlets, custom label library (Custom Tag Libraries) and XML. In addition, in this article, I will also use some other good stuff in the Jakarta project group, such as Tomcathttp: //jakarta.apache.org/tomcat/index.html (implementing the Java Servlet and JSP official Servlet containers, popular It is a JSP web server) and anthtp: //jakarta.apache.org/ant/index.html (based on Java-based automatic compilation ", this is a good stuff).

As a technician who has been developing many applications that have been using cutting-edge technology, I have always firm to understand new technologies and understand the logic development of this technology is crucial. But this is often a quagmire that trapted in the pace of learning. For this reason, I intend to use the complete process developed by Struts as the case of our teaching. This case, this case, "although the sparrow is small, the five internal organs", you can apply this process to your complex huge projects, at least in our big project, it is good.

Developers who have developed complex commercial applications know that customers' needs are always constantly changing, so if there is a set of standardized development processes to follow, when the customer puts forward new needs, we can at least clear which "unreasonable" demand is actually It is reasonable and feasible. Ok, let's take a whole process to you in my example.

The sample code in this article is part of the Strutssample application, including build.xml's full code to be here.

http://www.onjava.com/onjava/2001/10/31/examples/strutspartii.jar download.

Struts development process

As can be seen from the version number released from Struts, Struts is a new gathered, she has several parts, wise, if you don't know when the development is complete, it will make better use our development time. From the several Struts applications I have developed, I rigoveously summed up this more effective development steps:

1. Clear application needs;

2. From the perspective of user input and acquisition data, clear and design each user interface;

3. Determine the entry path of the user interface;

4. Determine the action mapping table (ActionMapping) by the application logic information;

5. Develop the class and application functions used by the user interface that is completed;

6. Develop an actionform and a corresponding data check method by data information in the user interface;

7. ActionMapping will be called to call the corresponding action or go to the corresponding JSP page. This step will be developed first.

8. Develop business application logic, is the corresponding JavaBean, EJB or other stuff;

9. Develop the corresponding JSP page for the system workflow defined by an actionmapping;

10. Complete system configuration file: struts-config.xml and web.xml;

11, compilation / test / release. Clear application needs

The first step in developing any application system is to collect user needs information. Regardless of whether a user logic looks reasonable, it is always possible to discover it more than it seems to be much more. Therefore, it is recommended to discuss a list of clear user needs, which is not just for the purpose of development, and can also analyze user needs through this table to determine which places may take more energy.

In our Strutssample project, the application needs are:

As a complete example of displaying the Struts framework, the function completed by this example is the user's login. The purpose is only to clarify the application of Struts, this example will not involve the security, database, EJB development, etc. related to the general complex application system.

Design user interface

In this application, including the following three user interfaces:

1) Login interface for username and password input;

2) Welcome interface when you log in to the user as a legitimate user;

3) When the error prompt interface is when the login fails.

Determine the entry path of the user interface

1) The login interface is the default page of this application;

2) Welcome interface only after successful login;

3) Any page that may have an error can enter the error prompt interface;

Determines the "Line Map" for the entire application determined by the application logic information, defines the ActionMApping for the configuration file struts-config.xml, and determine each user in the application by forwarding the process process. Request the corresponding action. Usually we gradually determine the information required for actionmapping during the development process, and the process of developing code is to improve the process of struts-config.xml by step by draft. When the Action class handles the user request, the Forward returned is one defined in an actionMApping. The Forward returned by an action is all possible, although an action generally defines several Forward. Then, if there are multiple Actions that may return the same forward, you can define it as global forward. This is similar to the global variable in the header file in C, if a Forward is not defined in the current Action description, then this global will work, the same The current defined Forward in an action will overwrite global definitions. In this simple instance we give, we define the overall Forward - "Error". When the forward returned is "error" map, then the errorpage.jsp page will be displayed to the user, although the current Action is not It is defined. We continue to develop, the project is increasing, and the project-related profile will be more and more detailed. In the following example, we will use the struts-confug.xml file used in Strutssample as an example, learn the definition of related mappings in the Global Forward and an action. The following defines an Action called "login", which is com.oreilly.Actions.LoginAction, and returns a Forward that is "Success" after the Action process user login is successful, the user will see Welcome. JSP page, if the login fails, Action will return the corresponding forward to display the login.jsp to the user, and if other errors occur during the process, the Action will return Forward - "Error", the user will see Error Tip ErrorPage.jsp. Property ->

The following only lists common attributes, please refer to org.Apache.struts.Action.ActionMapping related documentation

PATH - User request URI path corresponding to the current Action

TYPE - The full name of the current Action Java Class Name - the name of the ActionForm used in the current action, the specific information is defined in other parts of the configuration file.

Unknown - If this property is set to true, then this action will handle all the requests for the entire application, of course, there is only one action unknown property in an application system to be True.

SCOPE - ActionForm used in the action, can be "request" or "session", with the survival setting, the action is created in the corresponding time

INPUT - The relevant ActionForm in this action Gets the input page input by the user. When the ActionForm is set to automatically verify input data, it is found to return to the page when the improper data returns an error.

Validate - If this property is true, the Validate method corresponding to the Actionform before the Action action is automatically called, which is generally used to verify the data entered.

Forward element - define current action-related ActionForward

->

In the previous article, we have said that struts-config.xml is the Controller of the MVC mode. When determining configuration information in Struts-Config.xml, there should be more time to make a few more time to ensure that each Action definition and its correlation definition is in line with the application. If there is no detailed design in the project start, we will inevitably reach the code and configuration of each part when all code and configuration are integrated. Our current example Strussample is only an action that is only actions that only process the user login. How much is the size of the ACTION you want to use in an application system. Once the full set of Action is fully defined, we can develop its specific Action and Actionform classes, and gradually integrate the part of the completed part.

Developed by the design completed user interface to develop their class and application functions All ActionForm's implementation classes are subclasses of org.apache.struts.ArtionForm. An ActionForm is associated with the input form on the page, and the implementation of the ACTIONForm can also verify the legality of the user input data. As a Java Bean, ActionForm has a set and get method. When a form is submitted, the system will automatically call the SET method to place the data into the actionform, and the GET method will operate in the Action. In general, all data in the form is handled, and legitimacy verification can be completely delivered to the actionform. In applications, I personally tend to divide ActionForm and Action into different packages, because when you use a few pairs of ActionFrom and Action, you will be confused in a packet. The following code is the code of the ActionForm used in the instance.

/ * * LoginForm.java * / package com.oreilly.forms; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache .struts.action.actionform; import org.apache.struts.action.actionmapping; / ** * Verify two data to be used by users * * username - Log in user name * Password - user password * * / public Final Class loginform extends ActionForm {private String userName = null; private String password = null; / ** * Get method of userName * @return String * / public String getUserName () {return (userName);} / ** * userName Set methods * @param userName * / public void setUserName (String newUserName) {userName = newUserName;} / ** * password Get method * @return String * / public String getPassword () {return (password);} / ** * password of Set method * @param password * / public void setPassword (String newPassword) {password = newPassword;} / ** * reset all data * * @param mapping current Server ActionMapping * @param request currently being processed HttpServletRequest * / public Void Reset (ActionMapping Mapping, httpservletRequest request) {username = null; password = null;} / ** * Verify that the current HTTP request submitted data * If the data verification is discovered, it will return a package * all authentication error ActionerRors object * If data verified, the method returns null or a package without any validation * error * * @param mapping objects ActionErrors current ActionMapping * @param HttpServletRequest request currently being processed Server * / public ActionErrors validate (ActionMapping mapping, HttpServletRequest request) {ActionErrors Errors = new actionerrors (); // Current ActionForm, only need to check the username data IF entered by the user (username == null || username.length () == 0) {ErrorS.Add ("UserName", New Actionerror ("Error.Username.Required")));} Return (Errors);}}

The above code, only two points and the general Java Bean vary. One is the reset method, the value set in the method will restore the data of the table item to the default value on the form of the form when the form is reacted to its corresponding form. Its second is the Validate method, which is used to verify how the user enters data in the form. In the current example, we only verify the username entered by the user. Because a username whose password may be empty, our logic is not to check the password when verify. Verify the username, when the user name is found, the method generates an error object (ActionError). In Struts, use ActionerRors to load multiple errors, the "S" ending from ActionerRors, you can know that she is a collection of an ActionError object. When verifying the user input, you can verify that all the data in the form will be returned to the user through ActionerrorS, such a logic should be of course, it is impossible to have five different input errors, but It is necessary to divide five times, let the user modified to submit five times, huh, huh.

At the same time, we must know that in our example, we will prompt the user to use the ApplicationResource.Properties file. This file is used by the application in Web.xml when Tomcat is started. Usually, each application has a web.xml file in its web-infers, and the specific structural information on the deployment application, please refer to

Tomcathtp: //jakarta.apache.org/tomcat/index.html and other Server related user manuals.

A string of the prompt information you want to use in the ApplicationResource.properties file can define the string of the prompt information in the application, and the string is unique to determine its location by a key value. In our example, the string information corresponding to the key value error.Username.Required is "a username is required", and the string is displayed when the error message is displayed, and the error prompts determined by the key value are displayed. Through this mechanism, it provides a convenience of realizing multilingual in the system, translating these strings in the file into corresponding languages, our system can achieve version of Spanish, German, French, and Chinese, etc. .

The information in these ApplicationResource.properties is enough in our simple example:

login.title = Login Struts Sample error.userName.required = A username is required error.login.authenticate = Invalid username / password errors.footer =


errors.header =

Page Validation please color the folload error:
    ApplicationResources = Cannot Load Application Resources bundle {0}

    The header, buttons, or other parts of the page you need to have a text prompt can define the display string. We will explain how to get the display string from this file from this file in the last article of this series.

    to be continued

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

New Post(0)