Struts is a technical framework based on the Model 2 implementation, Model 2 is a Web application variant of the classic MVC (Model,view,control) model, which is largely due to the stateless nature of the HTTP protocol. The purpose of Model 2, like MVC, is to separate models and views using controllers to achieve loose coupling between different layers, and to improve flexibility, reusability and maintainability of the system. In most cases, you can equate Model 2 with MVC.
Figure 1 shows a typical MVC network application based on Java technology, from which the various parts of MVC correspond to the implementation techniques for Java EE.
Figure 1 MVC and Java EE technology
Before using Model 2, we have all the presentation logic and business logic together (such as the login.jsp in our top two topics), sometimes called this application model for model 1,model 1 main disadvantage is tight coupling, poor reusability, high maintenance costs.
Since struts is a framework based on MODEL2 implementations, the underlying mechanism is MVC, and we describe the concrete implementation of struts through Figure 2:
Figure 2 Struts MVC implementation
1. Framework Initialization
The Struts Framework Master controller (Actionservlet) completes all initialization work. The master controller is a servlet that is configured by Web.xml to automatically start the servlet, reading configuration information for the configuration file (Struts-config.xml), and initializing the corresponding Moduleconfig object for the different struts modules. The action mapping definition in the configuration file is saved in the Actionconfig collection, and other configuration information in the configuration file is saved in the Controlconfig collection, the Formbeanconfig collection, Forwardconfig collections and Messageresourcesconfig collections.
In particular, the initialization action completes automatically when the Web container is started, and after initialization completes, it intercepts all URL requests with the. Do end through the URL matching mapping.
2. The client sends an HTTP request
The user submits a request to the Web application by submitting the form or calling the URL, and the requested data is passed to the Web server on the HTTP protocol.
3. The master controller intercepts the request and instantiates the form Bean
The controller receives the HTTP request and finds the action subclass that should be requested from the actionconfig, and if there is no action, the controller sends the request directly to the JSP or static page. If there is a corresponding action and the action has a corresponding action form,actionform being instantiated and populating its properties with the data of the HTTP request, and then saving it in the servlet context (request or session), This allows them to be invoked by other action objects or JSPs.
In addition, you can also call validate () for data validation actionform after the data is populated, and you can return a Actionerrors object that contains all the error messages, if Actionerrors is not empty, The master controller returns the request directly to the portal page.
4. The Controller forwards the request to the specific action processing
The controller switches the request to a specific action based on the configuration information, and the form Bean is also passed to the Execute () method of the action.
5. Action to complete a specific business logic operation
The action is simple and typically contains only one Execute method, which executes the appropriate business logic and, if necessary, checks the data accordingly. After the execution is complete, returns a Actionforward object that the controller forwards by using the Actionforward object.
6. Action returns the target response object to the total controller
Action returns a target response object to the total controller based on the different results of the business process, which corresponds to a specific JSP page or another action.
7. The Master controller translates the HTTP request into the target response object.
The General controller converts the HTTP request to the target response object, based on the target response object returned by the business function action, which is typically a specific JSP page.
8. The target response object presents the results to the user
The target Response object (JSP) presents the results page to the user.
It is important that the client sends an HTTP request and finally gets an HTTP response through the Struts framework, which is the key to understanding the struts framework. Figure 2 depicts the structure of the struts framework, and Figure 3 describes the entire process of accepting a request until the response is returned through an activity diagram:
Figure 3 The middle process in which struts accepts and returns a response
Struts1.1 new Features
1. Multi-Module support
We know that in struts 1.0 you can only specify a struts profile (Struts-config.xml) for Actionservlet in Web.xml, which is certainly not a problem for a small system that only needs to be developed by one or two people, But if a large and medium-sized application is developed by many people, the problem arises. Because many developers may need to modify the struts configuration file at the same time, this will certainly result in a degree of contention for resources, and may be covered with each other, which will inevitably affect development efficiency and cause developers to complain.
In struts 1.1, in order to solve this parallel development problem, two solutions are proposed:
• Multiple configuration files
Supporting multiple profiles refers to the ability to specify multiple XML configuration files for Actionservlet and comma-delimited files, see the following example of a declaration for multiple struts profiles in Web.xml:
Code Listing 1 multiple Struts configuration files
1. The
2. Action
3. Org.apache.struts.action.ActionServlet
4. The
5. Config
6. The
7. /web-inf/struts-config.xml,/web-inf/book-struts-config.xml
8. The
9. The
1
One.
In this way, you can define a configuration file for each module, because the project is generally divided into modules, which greatly reduces the probability of conflict.
• Standalone Modules
However, there is a potential problem with multiple profiles: There is a conflict between different configuration files because multiple profiles are merged when Actionservlet is initialized. For example, a <exception> named Errordbaccess is configured in Struts-config.xml, and a similar <exception> is configured in Book-struts-config.xml, which creates a conflict.
To completely resolve this conflict, the concept of module (modular) is introduced in Struts 1.1. A module is an independent subsystem, corresponding to a separate configuration file, actionservlet the different modules of the configuration files in their separate Moduleconfig objects.
The following are the configuration methods for two separate modules:
Code Listing 2 Multi-module configuration mode
1...
2. The
3. Config
4. /web-inf/struts-config.xml
5. The
6. The
7. Config/book
8. /web-inf/book-struts-config.xml
9. The
10...
In this way, we configured two modules, one module named Config, and the other named Config/book.
• Dynamic Actionform Support
Actionform represents the data for an HTTP page form and can be viewed as a server map of the view page data, which is responsible for saving the data in the view for use by the controller or other views. In addition, it is responsible for validating data validation, so the Struts 1.1 document compares it to the firewall between HTTP and action, which is enough to reflect the actionform between the view and the controller.
As the actionform corresponds to the HTTP page form, your actionform will increase as the page grows. Dynamic Actionform (Dynaactionform) is designed to reduce the number of actionform that you do not have to create a specific Actionform class, just configure the desired virtual Actionform in the configuration file. This actionform is created dynamically by the struts framework through the configuration file. For example, code Listing 3 creates a dynamic actionform--loginform by specifying the type of <form-bean> org.apache.struts.action.DynaActionForm.
Code Listing 3 configures a dynamic actionform
1. The
2.
3.
4.
5.
6.
7. The
8. The
Dynaactionform saves the property in a Map object, while providing the corresponding get (name) and set (Name,value) methods, where the parameter name is the name of the property to be accessed, and value is an object. For example, to access bookname values in Dynaactionform, you can use string bookname = (string) get ("BookName") method, which is cast because BookName is stored in the map.
Because Dynaactionform is generated through a configuration file and does not have an entity object class, if you want to validate a dynamic Actionform object, you need to use Dynavalidatorform, which is a subclass of Dynaactionform. It provides the ability to dynamically actionform and dynamic form input validation. The validation rules are defined in the Validation.xml configuration file, and the corresponding implementation functions of these rules are defined in the Validator-rules.xml file.
• Implement exception handling through configuration
Struts1.1 allows for the configuration of exception handling, which can be configured to avoid handling exceptions in action by hard coding, thereby increasing the flexibility and maintainability of application exception handling. In general, an exception-handling object can be implemented by the following steps:
1. Implements Org.apache.struts.action.ExceptionHandler subclasses, overrides the Execute () method, handles the exception in the method, and returns a Actionforward object.
2. To configure exception handling objects in a configuration file, you can configure a global handler class or separate the handler classes for each action.
Code Listing 4 defines a global handler class Testexceptionhandler, which is used to handle all exceptions.
Code Listing 41 configuration of global declarative exception handling
1...
2. The
3.
4. handler= "Com.superAbc.TestExceptionHandler"
5. key= "Error.message"
6. Path= "/fail.jsp"
7. Scope= "Request"
8. Type= "Java.lang.Exception"/>
9. The
10...
The Type property defines a matching exception, and path defines the address that is forwarded after the exception occurs, and handler specifies the special handling of the exception before forwarding, and if handler is not provided, The default processing class Org.apache.struts.action.ExceptionHandler.