The front side summarizes the Struts2, Spring, and Ibaits frameworks separately. How can we use them together? This time, let's take a look at the integration and merger of Sturts2 and Spring. In fact, it is quite simple to import their own jar packages and connect to each other's jar packages. Just do a good job with each other.
Let's take a look at the Struts2 + Spring integration solution!
There are two integration schemes for Struts2 and Spring, which are divided by action creation!
Solution 1: Struts2 is responsible for the process, Spring is responsible for the creation of objects, Action is created by the Struts2 framework, and Service is created by the Spring framework. Take a look at the steps of its implementation example:
1. Integrate the Struts2 framework into the project and build the Struts2 framework environment. For details, refer to our Struts2 (1)-General Introduction. The steps are the same as those.
2. Integrate the Spring framework into the project to build the Spring framework environment. Here you can also take a look at Spring (1)-General Introduction. But here I want to talk about importing our jar package and copying the two configuration files to initialize the parsing applicationContext during project loading. and create various objects. We can add the framework listener in web. xml:
ContextConfigLocation
Classpath: applicationContext. xml
Org. springframework. web. context. ContextLoaderListener
3. In this way, even if the two environments are ready, how can they be integrated and merged? Here Struts2 provides us with the jar package: struts2-spring-plugin-2.1.8.1.jar. We need to reference this jar package to the project. In this way, the two frameworks can be integrated through the jar package.
Simply put, let's take a look at the role of this jar package: the plug-in provides a new factory class that covers the original factory class of Struts2. Working principle of the new factory class: First, check the Spring configuration file for consistent bean tag IDS Based on the complete Class Name of the Action. If there is a consistent description, the Action object is created by Spring, and there is a relationship between Spring assembly and combination objects. If no Action is found, special operations are performed. Then, the Strtus2 framework creates Action objects through reflection, and the automatic assembly function of the Strust2 framework is used to associate Action with Service objects. By default, the search is based on the name. Of course, we can also modify it. For example, we can change it to the type for search:
4. If the environment is set up, let's take a look at the code in each of our configuration files and focus on the notes:
A. The core configuration files of the two frameworks are configured in web. xml:
ContextConfigLocation
Classpath: applicationContext. xml
Org. springframework. web. context. ContextLoaderListener
Struts2
Org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
Struts2
/*
Login. jsp
B, struts2.xml, with the action class configured:
/Success. jsp
/Login. jsp
C. The service class is configured in applicationContext. xml:
The Code of action, service, and jsp can be easily accessed. As a matter of fact, it seems that this integration and merging method, Struts2 and Spring, are both independent, and the integration is not very good. Because Sping is a container, object management is more professional, and Struts2 is more professional in Process Control. Therefore, this method is rarely used and is not recommended. The second integration method is also a common integration method.
Solution 2: Struts2 is responsible for the process, Spring is responsible for the creation of objects, and Action and Service are created by the Spring framework. This is a common integration merge solution. The steps are basically the same as those above. They are the jar package for importing the response, the configuration file for the response, and the web. xml file. Let's take a look at the differences between the core configuration files of the two frameworks and the first solution:
1. struts2.xml:
/Success. jsp
/Login. jsp
2. applicatinContext. xml:
In this way, the merger of the two is realized, mainly by handing the action to Spring for creation, and combining the relationship between them. The framework of each other is only responsible for their own tasks, with a clear division of labor and improved efficiency.
In summary, for the simple combination of Struts2 and Spring, the example is relatively simple. We also need to understand in practice that the flexible application framework will become more efficient and simpler for our development. Through programming, through careful consideration of the charm of the Framework, and careful research, there will be more gains.