Differences between control layer SpringMVC and Struts2
We are now using the common MVC layer. The Controller layer of the control layer mainly consists of two frameworks: Struts2 and SpringMVC, which correspond to Action and Controller respectively. So what are the main differences between SpringMVC and Struts?
I. Framework mechanism
We know that Struts2 uses Filter, while SpringMVC uses Servlet.
Struts2 uses StrutsPrepareAndExecuteFilter, while SpringMVC uses DispatcherServlet. Filter is a special Servlet. The Filter is initialized after the container is started, and crashed after the service is stopped, later than the Servlet. The Servlet is initialized during the call and called before the Filter; the service crashes after it is stopped.
Ii. Interception Mechanism
1. Struts2
The Struts2 framework is a Class-level interception. Each request creates an Action. During integration with Spring, the Action Bean injection scope of Struts2 is prototype (otherwise thread concurrency problems may occur ), then, use setter and getter to inject request data to attributes.
In Struts2, an Action corresponds to a request and response context. When receiving parameters, the request can be received through attributes. This indicates that the attribute parameters allow multiple methods to be shared.
2. SpringMVC
SpringMVC is a method-level interception. A method corresponds to a Request context, so the method is basically independent and exclusive to request and response data. The url corresponding to each method at the same time, the parameter transmission is directly injected into the method, which is unique to the method. The processing result is returned to the framework through ModeMap.
During Spring integration, SpringMVC's Controller Bean adopts Singleton by default in Singleton mode. Therefore, by default, only one Controller is created for all requests, which should be non-shared, therefore, it is thread-safe. To change the default Scope, add the @ Scope annotation and modify it.
Iii. Performance
SpringMVC implements Zero Configuration. As SpringMVC intercepts methods, it loads a singleton mode bean injection. Struts2 is a Class-level interception. Each request corresponds to a new Action of the instance, and all attribute values need to be loaded for injection. Therefore, SpringMVC is faster than Struts2.
Iv. Interception Mechanism
Struts2 has its own Interceptor mechanism. SpringMVC uses an independent Aop method.
.