Franciscus, co-author of Struts Recipes, will introduce another major struts consolidation trick-this time, to import a struts application into the Spring framework. Follow George, who will show you how to change the struts action so that the management struts action is like managing Spring beans. The result is an enhanced web framework that makes it easy to take advantage of Spring AOP.
You must have heard of the control reversal (IOC) design pattern, because it has been spreading information about it for a long time. If you have used the Spring framework in any feature, you know how it works. In this article, I use this principle to inject a Struts application into the Spring framework, and you will experience the strength of the IOC model.
Consolidating a Struts application into the Spring framework has many advantages. First, Spring is designed to solve some real world problems with JEE, such as complexity, low performance and testability, and so on. Second, the Spring framework contains an AOP implementation that allows you to apply aspect-oriented techniques to object-oriented code. Third, some people may say that the Spring framework only handles struts better than struts handles itself. But that's the point of view, and I'm going to show you three ways to consolidate the Struts application into the Spring framework, and you decide which one to use.
The methods I have demonstrated are relatively simple to perform, but they have distinct advantages. I created an independent and usable example for each method so that you can fully understand each method.
Why is Spring so great?
is the founder of Spring, Rod Johnson, looking at Java in a critical light? Enterprise software development, and it is proposed that many enterprise challenges can be solved by strategically using the IOC model (also known as Dependency injection). When Rod and a dedicated open source developer team applied the theory to practice, the results produced the Spring framework. In short, Spring is a lightweight container that allows you to easily connect objects together using an external XML configuration file. Each object can receive a reference to a dependent object by displaying a JavaBean property, and the simple task left to you is simply to connect them in an XML configuration file.
Dependency Injection is a powerful feature, but the Spring framework can provide more features. Spring supports pluggable transaction managers, which can provide a broader range of choices for your transactions. It integrates the leading persistence framework and provides a consistent exception hierarchy. Spring also provides a simple mechanism for using aspect-oriented code instead of normal object-oriented code.
Spring AOP allows you to use interceptors to intercept application logic at one or more execution points. Enhancing the logging logic of applications in interceptors results in a more readable and useful code base, so interceptors are widely used in logging. You'll soon see that Spring AOP publishes its own interceptors to handle crosscutting concerns, and you can write your own interceptors.
Consolidate Struts and Spring
Like Struts, Spring can be implemented as an MVC. Both of these frameworks have their own advantages and disadvantages, although most people agree that Struts is still the best in MVC. Many development teams have learned to use Struts as the basis for structuring high-quality software when time is pressing. Struts has so much impetus that the development team prefers to integrate the features of the spring framework rather than convert it into spring MVC. It is good news for you that there is no need to convert. The spring schema allows you to connect Struts as a WEB framework to the business and persistence layer based on Spring. The final result is that now all the conditions are available.
In the next few tricks, you'll learn three ways to consolidate Struts MVC into the Spring framework. I will reveal the flaws of each method and compare their advantages. Once you understand the usefulness of all three methods, I'll show you an exciting application that uses one of the three methods I like best.
Three Tips
Each of the following integration techniques (or tips) has its own advantages and characteristics. I prefer one of these, but I know that all three of these can deepen your understanding of Struts and Spring. This will give you a broad range of choices when dealing with different situations. The method is as follows:
Using Spring's Actionsupport class consolidation Structs using Spring's delegatingrequestprocessor overlay struts's requestprocessor will struts Action Manage delegates to the Spring framework load application Environment
Whichever technology you use, you need to use spring's contextloaderplugin to load the spring application environment for Struts Actionservlet. Simply add the plugin to your struts-config.xml file just as you would any other plugin, as follows:
<plug-in className=
"org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property=
"contextConfigLocation" value="/WEB-INF/beans.xml"/>
</plug-in>
Each example provides a different way of consolidating Struts and Spring for a book Search application. You can see the main points of the example here, but you can also download the application to see all the details.