These three frameworks are in the order of the above, and in turn, they are all implementations of the MVC pattern. Why do these three, or even more, MVC frameworks appear? They are all designed to map the world of URLs to the Java world. Although they have different ideas of their internal implementation, they have their own advantages and disadvantages, but they have done their own purposes.
History Introduction
Most of the Web applications are running on HTTP. The HTTP protocol is a series of stateless text transfer protocols. Stateless protocols do not log relationships between multiple requests that are received, that is, how the server corresponds to the corresponding client is a problem. In addition, HTTP is text-based. How to match text-based technology to strong-type Java requires a lot of data binding work.
The HTTP protocol was not originally designed to meet the needs of Web application developers, but was designed to request and process static HTML documents.
The Java servlet API solves these problems, and the servlet encapsulates the details of client/server interactions directly through a set of object-oriented abstractions. Instead of parsing the HTTP request ourselves, we work with a packaged Java object.
Ava programmers can write HTTP server code based on HTTP client/server communication in an intuitive object-oriented, abstract way. The core objects of the Servlet API are Servlets, requests, and corresponding. A servlet is a single-instance Java object.
But using servlet programming requires a lot of "reinventing the wheel". such as request parameters to the Java type of data binding, data validation, interface rendering, etc., need to be written manually.
These three frameworks, based on this, have been packaged to simplify development. Let's take a look at their differences.
Struts1
Now STRUT1 use less, but there are some old projects in the use of struts1, because I also used the framework, but also to do a comparison:
1, the server starts, the Web application will load when it starts to initialize Actionservlet and chronicle struts configuration file (struts-config.xml), read configuration information into memory, for later action call
2, the user sends a request to the server through the client, http://localhost:8080/struts_login/login.do
3. Tomcat will create HttpRequest and HttpResponse instances, and invoke the Doget or Dopost method of the central controller according to the user method request;
We have configured all requests in Web. XML that conform to a particular format to be handled by the servlet specified by struts. For example, a request with a. Do End (*.do) is provided by the Org.apache.struts.action.ActionServlet to handle it. Actionservlet will get the user's request, and to analyze the Url,actionservlet Central controller will intercept/login. After the truncation, it is to go to struts-config.xml this config file to find the <action> tag the value of the Path property equals the action of the Section, and the information inside the action tag is placed inside the actionmapping.
4, according to the name in actionmapping to find Actionform, if configured Actionform, then to the request or session to find, If a actionform has already been created in the request or session, it will be returned, and if it does not exist, it will be created with reflection based on the Actionform completion path. Then put the created actionform into the reqeust or session.
5, first perform actionform in the Reset method to reset, and then get the name of all the input fields in the form, and then call Request.getparametervalues (), according to the name of the corresponding value, Finally, the data in the form is placed in a map, the key in the map is the name of the form input field, the value of map input field values (string array), next call a third-party component Beanutils, the value in the map, Set to Actionform based on the setter method in Actionform.
6, based on the completion class name of the action to find in the map, if there is a return action object, otherwise based on the full name of the action class with reflection to create, and then put the created action into the map. So Struts1 's action is single-instance, and there is a thread-safety issue.
Once the corresponding action is found, Actionservlet will send the data submitted by the form to the Actionform class (which generates the corresponding call Set/get method) to the Name property value of the corresponding action tag in Struts-config, if any, [ Actionform subclasses, and configured in the Form-bean tag, to perform data validation can overwrite the Validate method in Actionform, the recommended use of JS, reduce the burden on the server]). The Actionform and the current HttpServletRequest object are injected into the action method of the generation call.
7. Execute the Execute method in user-defined action, pass the Actionmapping,actionform,request,response to the past, and return the Actionforward to Actionservlet.
8. Based on the returned Actionforward, the Actionforward object is switched to the JSP page corresponding to path based on the <forward> match name of this action configuration.
struts2
is based on the filter interception implementation, the implementation diagram is as follows:
The access process is as follows:
1, the Tomcat server starts, when the Web application starts, The Web. xml file is loaded first, then the filter filter is initialized, and the Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter listener is initialized. At this point, the STRUTS2 configuration file will be read into memory (if it is Struts2 and spring integration, will be listner first start, spring completes the Struts2 acion creation, does not delay the initialization of the Struts2 file) for later action call ;
2. The user accesses the browser, sends the request, and points to the servlet container.
3. The container tomcat creates HttpRequest and HttpResponse instances and intercepts and obtains the name of the controller through the Web. xml file mapping request.
4. Then the container invokes Strutsprepareandexecutionfilter,strutsprepareandexecutefilter to determine the information for the action by Actionmapper
5, if Actionmapper decided to call a action,strutsprepareandexecutefilter to the processing of the request to action Agent Actionproxy;
6. Controller Call Actionproxy
7, actionproxy through ConfigurationManager read Strut.xml configuration file to get action and interceptor information, find the action class that need to call;
8, Actionproxy the request to the Actioninvocation object;
9, the Actioninvocation instance uses the command pattern to invoke, in turn invokes the action and the Interceptor;
10, according to the configuration information of the action, generate Result,result information returned to Actioninvocation.
11, return the result to the client.
Characteristics:
1, the threading mode, each request will produce a new instance processing request, the multi-threaded environment does not have the data synchronization problem;
2, the introduction of data-dependent injection "page form data injection into action injection, instance object injection, through the Set method injection"
3, based on the AOP interceptor, can be flexibly controlled before and after each request;
4, configuration file support form, based on the Convention is greater than the configuration, simplified configuration;
5, built-in plug-in form to support Ajax such as dojo, support a variety of last-class display JSP, Freemarker,veiocity and so on.
Springmvc
SPRINGMVC is implemented based on servlet interception, mainly by the front-end controller (dispatcherservlet), the mapping Processor (handlermapping), the adapter (handleradapter), Interceptors (Handlerinterceptor), Controllers, view parsers (Viewresolver), Views (view) are composed of the parts. Here's a look at Springmvc's workflow.
1, the Tomcat server starts, when the Web application starts, it will first load the website. xml file, then start Listenr listening, initialize the CONTEXTLOADERLISTENR listener. At this point, some of the spring configuration is loaded, and the spring configuration is initialized to ApplicationContext;
2. Then initialize the servlet and initialize the Dispatcherservlet. This completes the initialization of the ServletContext and completes the initialization of the Webapplicationcontext in the ServletContext, and reads the configuration information into memory;
3, the user through the browser access, send the request. The Tomcat container creates HttpRequest and HttpResponse instances based on the request, and invokes the Doget or Dopost method of the central controller based on the method request of the user.
We have configured all requests in Web. XML that conform to a particular format to be handled by the servlet specified by SPRINGMVC. For example, any request that ends with a. Do (*.do) is handled by Org.springframework.web.servlet.DispatcherServlet. The entrance to the SPRINGMVC is also the core.
4, Dispatcherservlet, parse the URL of the request, get the request resource Identifier (URI). Then, based on the URI, call handlermapping to get the related object of the handler configuration (including the handler object and the interceptor corresponding to the handler object), and then return it as a Handlerexccutionchain object;
Handlermapping has the following two implementations:
A, simpleurlhandlermapping, through the configuration file, to map a URL to the controller
b, defaultannotationhandlermapping to map a URL to the controller via annotations
3, Dispatcherservlet According to obtain the handler, chooses a suitable handleradapter. (If Handleradapter is successfully obtained, the Interceptor Prehandler method is started);
4. Extract the model data from the request, populate the handler entry, start the execution of handler, controller, in the process of filling handler, according to your configuration, spring will do some work:
Httpmessageconveter: Converts a request message (such as JSON, XML) to an object, converting the object to the specified corresponding information;
Data transformation: Data conversion for a request message. such as spring converted to integer, double, etc.
Data formatting: Data is formatted for the request message. Convert a string to a formatted number or a formatted date
Data validation: Verify the validity of the data (length, format, etc.), and the acceptance results are stored in Bindingresult or error
5, the controller after the business call, to Dispatcherservlet, will return a Modelandview object;
6, Dispatcherservlet According to the returned Modelandview, query one or more viewresolver view parser, find the object specified by the Modelandview objects;
7. Returns the rendered result to the client.
Characteristics:
Threading Mode: Single case mode, good efficiency;
Method-level-based interception, rather than class-based. More granular and flexible control;
The annotations simplify multiple configurations, but add a lot of code to the code that is irrelevant to the business.
Compare
Despite the security flaws in the older versions of Struts2, STRUTS2, and Springmvc are still a very popular MVC framework, and STRUT1, though most efficient, is now less used. So, let's take a look at their similarities and differences.
Of these three frameworks, the efficiency of STRUTS1 is second only to JSP, the most efficient among them. But Struts1 is oriented toward abstract class programming, not interface-oriented programming. STRUTS2 provides a base class called Actionsupport, which is not required, but is basically inherited if you want to use the functionality of STRUTS2. SPRINGMVC is interface-oriented programming that requires implementation of an interface (configuration file).
The entry point of the STRUTS1 is the SERVLET,STRUTS2 entry point is the FILTER,SPRINGMVC entry point is the servlet;
STRUTS2 is class-level interception, each request creates an action, and then injects the data from the request by invoking the setter method; Springmvc is a method-based interception, a method that corresponds to a request context, and a method that corresponds to a URL ;
The class properties of Struts2 are shared by all methods, which means that they cannot be identified by annotations and are independent of the methods, but all action variables are shared. The Springmvc method is basically independent, sharing the request data, requesting the data through the parameters, the processing result is returned to the framework through MODELMAP, and the method does not share the variables.
Interceptor Implementation mechanism: STRUTS2 has its own interceptor mechanism, SPRINGMVC uses the independent AOP way. Therefore, although the spring configuration can inherit, but the configuration amount is still greater than SPRINGMVC;
The SPRINGMVC configuration is based on the IOC container, which facilitates SPRINGMVC expansion and enhancement on the one hand and facilitates integration with spring, while the integration of STRUTS2 with spring requires a third-party package from spring.
Struts2 inside can have no httpservletrequest and HttpServletResponse, only getter, setter or actioncontext data, this will make action more independent. The entire web has nothing to do with the action, which can run independently in any environment, meaning it can be reused to the maximum. Instead of hiding httpservletrequest and httpservletresponse like Struts2, Springmvc is doing it in a way that parameters are passed into the controller.
The action of the STRUTS1 is the same as the controller of the SPRINGMVC, and is not thread-safe. This means that the system will use the original instance to process each request. This reduces the time of object creation and garbage collection, but when processing multi-threaded calls, the controller is not thread-safe, while STRUTS2 is thread-safe.
Struts1, Struts2 and SPRINGMVC