In-depth study of StruCTS

xiaoxiao2021-04-10  355

For Struts how to control, process the customer request, let us specify the four core components of Struts. These components are: ActionServlet. Action Classes, Action Mapping (here included ActionForward), ActionFrom Bean. Struts ActionServlet controller object ActionServlet inherits from Javax.Servlet.http.httpservlet class, which is the role in Struts Framework is a central controller. It provides a central location to process all of the terminal requests. The controller ActionServlet is mainly responsible for assembling the HTTP's client request information, forwards to the appropriate processor based on the specified description of the configuration file. Follow the Servelt standard, all Servlet must be declared in the web configuration file (web.xml). Similarly, the ActoInServlet must be described in the Web Application Profile (Web.xml), and the configuration information is as follows. action org.apache.struts.Action.ActionServlet All requests URI exists with * .do And mapped to this servlet, which is configured as: action *. Do A mode The request URI meets the following format: http://www.my_site_name.com/mycontext/ActionName.do Center controller provides a centralized access point for all representation layer requests. The abstract concept provided by this controller reduces the difficulties of developers to build public application system services, such as management views, sessions, and form data. It also provides a general mechanism such as error and abnormal processing, navigation, internationalization, data verification, data conversion, and more. When the user submits a request to the server, the actual information is first sent to the controller ActionServlet, once the controller obtains the request, it will pass the request information to some auxiliary classs. These assisted classes know how to process business operations corresponding to request information. In Struts, this auxiliary class is org.apache.struts.Action.Action. Usually developers need themselves to inherit the Aciton class, thereby implementing their own action instances. Struts Action Classes ActionServlet is commissioned to the RequestProcessor object by the controller all submitted requests. RequestProcessor uses the struts-config.xml file to check the request URI to find the action Action marker. The role of an Action class, like an Adaptor between the client request action and the business logic processing, and its function is to separate the request with the business logic. Such separation allows the customer request and the ACTION class to have multiple point-to-point mappings.

And the Action class typically provides other auxiliary functions, such as Authorization, Logging, and Validation. public ActionForward execute (ActionMapping mapping, ActionForm form, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response) throws java.io.IOException, javax.servlet.ServletException Action is most commonly used is execute () method. (Note that the previous Perform method is no longer supported in Struts1.1), and there is an Execute () method, please refer to APIDOC, not described herein. When Controller receives a customer request, when the request is transferred to an Action instance, if this instance does not exist, the controller will first create, then call the Execute () method of this Action instance. Struts framework only creates an instance for each Action class in the application system. Because all users use this instance, you must determine your Action class in a multi-threaded environment. The following figure shows an Execute () method how to be accessed: an Execute () method of the Action instance Note that the customer's own Action subclass must override the Execute () method because the Action class is returned by default. Struts action mapping talked about how a customer request was forwarded and processed by the controller, however, how does the controller know what information forwarded to what kind of Action class? This requires some mapping configuration instructions corresponding to the action and request information. In Struts, these configuration mapping information is stored in a specific XML file (such as struts-config.xml). These configuration information is read in memory when the system is started, and the Struts Framework is used during operation. In memory, each element corresponds to an instance of the org.apache.struts.Action.ActionMapping class. The following table shows a login configuration mapping.

above the configuration: When the /Logonaction.do can be submitted by /LogonAction.do (assuming the configured controller map to * .do) to submit request information At the time, the controller entrusts the information to a com.test.logonaction process. Call the execute () method of the LogonAction instance. At the same time, the mapping instance and the corresponding LogonForm bean information are incorporated. Where Name = LogonForm, the Actionform Bean declared by the form-bean element. The declaration of Form-beans is shown below. Using an actionforward navigation element indicates that the execute () method of the Action instance is running or, the controller can transfer the response information to the appropriate place according to mapping. As in reality above, if the customer logs in success, call Welcome Forward to return the success information to the /welcome.jsp page. The following instance code can be used to return Welcome Forward at the end of your execute () method. Of course your Welcome Forward must be defined in the Action element properties, as declared above. Return (Mapping.FindForward ("Welcome"); ActionForward object is a configuration object. These configuration objects have unique identifiers to allow them to retrieve them according to meaningful names such as "Success", "failure". The ActionForward object encapsulates the forward URL path and is requested by the processor to identify the target view. The ActionForward object is built from element located in Struts-Config.xml. Below is an example of element examples of Struts, belonging to the range of element.

Based on the result of executing the execute (...) method of the request processor, when passing a value matching the value of the Name property in the element, the next view is You can use the developer in the Execute (...) method org.Apache.Struts.Action.ActionMapping.FindForward (...). The ActionMapping.FindForward (...) method provides an ActionForward object from its local range and from the global scope, which returns to the RequestProcessor to call the next view in RequestDispatcher.Forward (...) or response.sendredirect (...). When the element has the redirect = "false" property or the redirect property does not exist, RequestDispatcher.Forward (...) is executed; when redirect = "true" is, the sendRedirect (...) method will be called. The following example exemplifies the usage of the redirect property:

If redirect = true, the URL established, such as / contextPath / path, explains the URL in httpservletresponse.sendredirect (...) The root directory of the servlet container. If redirect = false, URI established, such as / path, because servletContext.getRequestDisptacher (...) uses virtual directory related URLs. Slightly talk about the concept of Global-Forwards. It describes the ActionForward that can be used throughout the application in the configuration file instead of just a specific action. struts anctionsform bean capture form Data When you explain the ActionServlet, Action Classes, and Action Mapping, we mention the concept of Actionform Bean. The non-persistent data store for the message transfer (or state transfer) of an application system is maintained by the Actionform Bean. Actionform derived objects are used to save the parameters of the request object, so they are closely linked to the user. An ActionForm class was established by RequestProcessor. This occurs in a form that is completed forward to a URL, which is mapped to the controller servlet rather than the form properties specified by JSP and corresponding action mapping. In this case, if you don't find it within the specified activity range, RequestProcessor will try to find it to cause a new ActionForm object? / TD>

RequestProcessor will then rearrange the form attribute, populate the form with the request, and then call the Validate (...) method of the form object to perform the server-side user input verification. The validate (...) method is called only when the Validate property is set to TRUE only in the ActionMapping object; this is the default behavior. Request.getParameterValues ​​(Parameters) is used to get a string [] object, which is used to form a form fill; the result of the verification should be an ActionerRors object, with org.apache.struts.taglib.html.errostag to display the verification error to the user. ActionForm can also be used to save the current user will be referenced by an intermediate model state that references referenced by a view. When a form object is found by RequestProcessor, it is passed to the Execute (...) method of the request processor. An ActionForm object can also be established by requesting processors. The form object establishment is to provide an intermediate model state to use the request range JSP; which will ensure that the object does not still exist after the validity expires. By default, all forms are saved as sessions. The presence of the form object in the session may result in waste memory, the same, the request processor must track the lifecycle of the form object saved in the session. A good capture form data is to use a separate form bean for related forms across multi-user interactions. Form beans can also be used to store intermediate model states that can be changed by custom tags during feedback. The label usage is avoided in the view, so it is a good task division, the web production group mainly handles the flag, and the application development group mainly handles the Java code. Label factors exit logic accessed the intermediate model status; when accessing nested objects or when this logic can be complex when listed by aggregate. Note: In Struts1.1, the check function of ActionForm is gradually stripped (of course, can still be used). Using Validator Framework for unified management of form data authentication of the entire application system. I believe the information, please refer to: http://home.earthLink.Net/~dwinterfeldt In the use of ActionForm, Struts advocates the value object (Value Object). This makes it possible to understand and use the customer or developers to more clearly understand and use the data status and object state. For each customer request, Struts framework is generally needed to experience the following steps: (1) Check the Action mapping, determine that the ACTIONMM is configured (2) Find Form Bean according to the Name property Configuration Information (3) Check the range of FORMBEAN of the action, determine if there is an instance of this form bean at this range. (4) If there is an instance of this FORM BEAN, it has existed the present request, which is the same type of time, then reuse. (5) Otherwise, re-construct an instance of a FORM BEAN (6) method () method () method for mod (7) Call the corresponding setter method, assigning a status attribute (8) If the validated attribute north is set to true, Then call the FORM BEAN's validate () method.

(9) If the validate () method does not return any errors, the controller transmits ActionForm as the parameter to the Execute () method of the Action instance and execute. Note: The reset () and validate () methods inherited directly from the ActionFrom class, do not implement what processing, so it is necessary to re-overwate themselves. Struts' other components Struts framework itself provides a lot of scalable components or Sub Framework, which is convenient for developers to build a Web layer on its architecture. Such as Upload, Collectes, Logging, and more. Let's take a look at two more important components: ValidationG framework and struts taglib. For other components, please refer to the Struts User Manual (http://jakarta.apache.org/struts/userguide). Validation Framework for Struts In Struts1.1, the Validation Framework has been added. Increased verification of FORM data submitting. Verify the validate () required for an actionFrom bean is verified by the profile of the Validate (). For more information, please refer to http://home.earthlink.net/~dwinterfeldt. Personal recommendations can adopt this configuration method for small applications, but there is a system with a large Web layer form application in the application system, and the business requirements are relatively large, using the Validation Framework may aggravate the development difficulty, system maintenance difficulty. You can learn from the Validation Framework's JavaScript Validator Tag. The Struts Taglib Struts provides a set of scalable custom tag libraries that simplify the process of creating user interfaces. Currently include: Bean Tags, HTML Tags, Logic Tags, Nested Tags, TEMPLIB. For the structure and use of Struts Taglib, you can refer to the previous introduction of CUTOMER TAG LIB. For details, please refer to BeanUtils component is bean Introspection Utilites. It is a Jakarta Commons project group. Mainly to help build JavaBean properties (Getter, Setter), which already provides a dynamic definition and access to the properties of the bean. For more information, please refer to. Http://jakarta.apache.org/commons/beanutils.html If you are interested in this, you can refer to some information about Java Reflection (Reflectio). The component of Collectes is mainly provided with some collection or list objects, and the extension is performed on the basis of the original Java Collections Framework. For details, please refer to: http://jakarta.apache.org/commons/collections.html and http://cvs.apache.org/viewcvs/~checkout ~/jakarta-commons/collections/status.html?rev=1.13 Digester This component is translated into Chinese meaning "assembly".

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

New Post(0)