Home  >  Article  >  Java  >  The difference between control layer SpringMVC and Struts2

The difference between control layer SpringMVC and Struts2

黄舟
黄舟Original
2017-03-03 10:11:101566browse

Our popular MVC layer and control layer frameworks mainly include two frameworks: Struts2 and SpringMVC, which correspond to Action and Controller respectively. So what are the main differences between SpringMVC and Struts?

1. Framework mechanism

We know that Struts2 uses Filter to implement, while SpringMVC uses Servlet to implement.

Struts2 uses StrutsPrepareAndExecuteFilter, while SpringMVC uses DispatcherServlet. Filter is a special Servlet. Filter is initialized after the container is started; it crashes after the service is stopped, later than Servlet. Servlet is initialized when called, before Filter is called; it crashes after the service stops.


2. Interception mechanism

1. Struts2

The Struts2 framework is a class-level interception. Each request will create an Action. When integrated with spring, the Struts2 Action The Bean injection scope is the prototype mode prototype (otherwise thread concurrency problems will occur), and then the request data is injected into the property through setters and getters.

In Struts2, an Action corresponds to a request and response context. When receiving parameters, it can be received through attributes. This shows that attribute parameters are shared by multiple methods.


2. SpringMVC

SpringMVC is a method-level interception. One method corresponds to a Request context. Therefore, the method is basically independent and has exclusive access to request and response data. Each method corresponds to a URL at the same time. The parameter passing is directly injected into the method, which is unique to the method. The processing results are returned to the framework through ModeMap.

During Spring integration, SpringMVC’s Controller Bean defaults to Singleton mode, so by default, only one Controller will be created for all requests, and there should be no shared attributes. , so it is thread-safe. If you want to change the default scope, you need to add the @Scope annotation to modify it.


3. Performance

SpringMVC implements zero configuration. Due to SpringMVC’s method-based interception, singleton mode bean injection is loaded once. Struts2 is a class-level interception. Each request for a new Action corresponding to the instance requires loading of all attribute value injections, so SpringMVC is faster than Struts2.


4. Interception mechanism

Struts2 has its own interception Interceptor mechanism , SpringMVC uses an independent Aop method.

The above is the difference between the control layer SpringMVC and Struts2. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn