Struts Framework works and components

xiaoxiao2021-04-06  307

Struts anctionsRVlet controller object

ActionServlet inherits from Javax.Servlet.http.httpservlets, which played 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 mode and map to this servlet, which is configured as follows:

Action

*. do

The request URI of this mode meets the following format:

http://www.my_site_name.com/mycontext/Actionname.do

The central 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

The ActionServlet will be 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.IOEXCEPTION, JAVAX.SERVLET.SERVLETEXCEPTIONACTION is the most commonly used 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 figure below shows how an Execute () method is accessed:

Execute () method of an example

Note that the Action subclass of the customer inherits himself must override the execute () method because the Action class returns NULL by default.

Struts action mapping

Tell the customer request how to be forwarded and processed by the controller, however, how does the controller know what information forwarding 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.

TYPE = "com.test.logonaction"

Name = "logonform"

Scope = "request"

INPUT = "logoncheck.jsp"

Validate = "false">

TYPE = "com.test.loginform" />

The above configuration indicates that when the request information can be submitted via /logonaction.do (where the configured controller map is assumed to be mapped to * .do), the controller entrusts the information to com.test.logonAction. 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. Use an actionforward navigation

Element indicates that when 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");

The 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.

Type = "packagename.editcustomerProfileAction"

Name = "CustomerProfileform" Scope = "Request">

Based on the result of executing the executor's EXECUTE (...) method, when a value matching the value specified in the element, the next view can be used in the Execute (...) method for convenient 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 ActionForm Bean captures form data

We all mention the concept of an actionform bean when explaining the ActionServlet, Action Classes and Action Mapping. 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, the RequestProcessor will try to find a form bean that could result in a new ActionForm object. This ActionForm object is found in the specified activity range of the Name property of the element;

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 required to experience the following steps when processing the ActionForm:

(1) Check the Action mapping, determine the map of ActionForm already configured in the action.

(2) Find the configuration information of Form Bean according to the Name property

(3) Check the range of use of the Action of FormBean, 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-build an instance of form bean

(6) Form bean's reset () method is called

(7) Call the corresponding setter method, assign the status attribute

(8) If the property of Validate is set to TRUE, then the FORM bean's validate () method is called.

(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.

Other components of Struts

The Struts Framework itself provides a lot of scalable components or Sub Framework, which is convenient 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 is 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.

Struts taglib

Struts provides a set of scalable custom label libraries (Taglib) that simplifies 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, please refer to

Beanutils

The full name of this 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).

Collectes

This component mainly provides some collection or list objects, and extension 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 means "assembly". Its main function is some Java class objects of the system based on the XML configuration file. Digester helps you specify the map model between XML and Java objects and allows the customer to customize mapping rules (Rules). Please refer to the details

http://jakarta.apache.org/commons/digester.html

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

New Post(0)