In the previous article, I introduced in detail how to build a maven environment and generate a maven skeleton web project. In this chapter, I will describe the process structure of Spring MVC, differences Between Spring MVC and Struts2, and analysis of some configuration files in the example.
I. Introduction to Spring MVC 3.0
Spring MVC is a typical MVC Framework and is a built-in Web framework of Spring. It can be used as a presentation layer for application projects. After Spring 2.0 makes major upgrades to Spring MVC, spring 2.5 introduced the annotation-driven feature for Spring MVC. In the 3.0 era, it fully supports REST network services and easier network programming. This series of changes will undoubtedly attract us into the Spring MVC 3.0 world.
The REST feature is added to Spring MVC 3.0. It accesses system resources through URLs without extensions. REST regards all accessed resources as static, one or one group, and each URL address is a static resource. So how does Spring MVC 3.0 support REST? Simply put, it is provided through the @ RequestMapping and @ PathVariable annotations. You can process the corresponding request by specifying the value and method in @ RequestMapping. In addition, the spring mvc framework has done a lot of work.
Ii. Spring MVC Process
According to the introduction in the official document and their own understanding, the elephant drew a Spring MVC process diagram. You can refer to it.
1. The core of Spring MVC is DispatcherServlet. When a client sends a request, the request is processed by a series of filters. Then DispatcherServlet will receive this request.
2. DispatcherServlet searches for the Controller that matches the request from the HandlerMapping object and returns the result to DispatcherServlet.
3. DispatcherServlet forwards the request to the target Controller. If an interceptor is defined, the request will be processed by the interceptor.
4. After the Controller completes the business logic, a result is returned to DispatcherServlet.
5. DispatcherServlet queries ViewResolver Based on the result, finds the corresponding view object, and returns the result to DispatcherServlet.
6. Based on the specified display result, DispatcherServlet calls the template object to render the view.
7. Return the view to the client.
According to the above description, it is obvious that the core of Spring MVC is Servlet, and the Controller created is actually a Servlet.
Iii. Comparison between Spring and struts2
Another well-known MVC framework is Struts2. The core of Spring MVC is Servlet, while the core of Struts2 is Filter. The following table lists the main differences and comparison results between Spring MVC and Struts2.
Through the above comprehensive comparison, Spring MVC 3.0 has much more advantages than Struts2. Although it still has some shortcomings, it will inevitably be improved with the upgrade of later versions, it will do better. Therefore, using Spring MVC 3.0 as the presentation layer is better than Struts2.
Iv. Thread Security
Spring MVC uses Singleton by default, which leads to a potential security risk. The core issue is the persistence of instance variables.
There are two solutions to this problem:
A) no instance variables are used in the controller.
B) Change the Controller scope from Singleton to prototype.
These two methods are both good and bad. First, developers need to have a high level of programming and ideology, and strive to avoid such bugs during the coding process, the second is that the container automatically generates an instance for each request, and the JVM recycles the garbage. Therefore, thread security is achieved. The advantage of using the first method is that there is only one instance object, and all requests call this instance object. The speed and performance are better than the second method, that is, the programmer needs to control the status persistence of instance variables on his own. Second, because each request creates an instance, it consumes a lot of memory space.
V. Configuration File
Spring MVC is an integral part of Spring, so the configuration file will become much simpler. The following are the most important configuration files in this example.
1. pom. xml
Manage Project dependencies, compile, publish, and configure plug-ins. All the dependent packages are determined by the configuration. In addition, other dependencies of the dependent packages are not required. maven will automatically obtain and manage them, which will undoubtedly reduce the workload, there is no need to look for jar packages everywhere, or the version is inconsistent.
Define dependency version attributes
Spring dependency. This example is a basic example, so it is enough to add more extensions in the future.
MyBatis dependency, and iBatis is changed to MyBatis from version 3.0. In this example, elephants do not use Hibernate, but use MyBatis, which is more lightweight, as a persistent layer framework. It is very simple and flexible to use. In addition, a mybatis-spring plug-in is used in this example. This is because spring 3.0.5 only supports ibatis 2.0. Therefore, this plug-in is required to process underlying data sources.
This example uses AOP, so these two dependencies are required.
The following are some other necessary dependencies. It is worth noting that, in this example, elephants still use classes for services and do not implement interfaces, therefore, cglib is required. In addition, the page uses html as the display layer and freemarker labels to process dynamic data.
2. web. xml
The main difference from the web. xml of ssh2 is to replace the Struts2 STARTUP configuration with the Spring MVC setting. The configuration is as follows:
The servlet-context.xml contains the content to be executed when the container is started, and the service-context.xml is followed by a spring context listener to scan it. The two configuration files can be combined into one. To facilitate management and use the spring context, elephants write two files. If we do not specify the init-param configuration, by default, when the server is started, it will look for the naming rule in the WEB-INF directory as <servlet-name>-servlet. xml file, corresponding to this is the ssm3-servlet.xml file, the elephant unified all files are placed under the classpath.
3. servlet-context.xml
The configuration file for Spring MVC startup, including component scanning, url ing, and setting freemarker parameters, so that spring does not scan classes with @ Service annotations. Why do we set it like this? Because the servlet-context.xml and service-context.xml are not loaded at the same time, if this setting is not done, spring will scan all classes with @ Service annotation into the container until the service-context.xml is loaded, because the container already has a Service class, cglib does not proxy the Service. The result is that the transaction configuration in service-context does not work and an exception occurs, data cannot be rolled back. In addition, the rest url can be resolved to the DefaultAnnotationHandlerMapping class for request ing. at startup, it puts all the methods marked with @ RequestMapping annotation in the Controller Into A HandlerMapping object, when there is a request, the object will be searched for whether there is a matching path processing method. If yes, if Not, a Not Page Found warning will be output.
The display layer uses the freemarker template engine. For ease of editing, elephants use html as the display page. The Spring framework integrates and encapsulates freemarker, which is easy to configure. It mainly defines the FreeMarker view parser and attribute configuration. There are a lot of introductions on the meaning of these attributes on the Internet, so the elephant will not be here, spring MVC will be explained in combination with the code later. Spring MVC supports multiple types of view files. Such as jsp, freemarker, velocity, tiles, and jasperreports.
4. service-context.xml
And the application. there is little difference between xml, mainly to change the hibernate part to mybatis, And to configure the base class of MybatisDao so that the Service class can be introduced by annotation, and then scan the package path, do not scan classes with @ Controller annotations. Because these classes have been scanned in servlet-context when they are started with the container.
5. mybatis-config.xml
The main configuration of mybatis contains the POJO ing file. Let's take a look at it here. The following sections will describe it. Friends who are familiar with ibatis will be very simple and can skip it directly.