Use spring to better handle struts actions

Source: Internet
Author: User
You must have heard of the IOC design pattern, because information about it has been circulating for a long time. If you have used the Spring framework in any function, you will know the role of the framework. In this article, I am using this principle to inject a struts application into the spring framework, and you will personally experience the strength of the IOC mode.
  
Integrating a struts application into the spring framework has many advantages. First, spring is designed to solve some real-world problems related to Jee, such as complexity, low performance, and testability. Second, the Spring framework contains an AOP implementation that allows you to apply Aspect-Oriented technology to object-oriented code. Third, some may say that the Spring framework only processes struts better than struts. However, this is a point of view. After I demonstrate three methods to integrate Struts applications into the spring framework, you can decide which one to use.
  
The methods I demonstrate are relatively simple to execute, but they have obvious advantages. I have created an independent and available example for each method, so that you can fully understand each method. See the download section to obtain the source code of the complete example. See references to download struts MVC and spring frameworks.
  
Why is spring so amazing?
  
Rod Johnson, the founder of spring, looks at Java from a critical perspective? Enterprise software development, and proposes that many enterprise problems can be solved by strategically using the IOC model (also known as dependency injection. When rod and a team of dedicated open source developers applied this theory to practice, the Spring framework was created. In short, spring is a lightweight container that can be used to easily connect objects together using an external xml configuration file. Each object can receive a reference to the dependent object by displaying a JavaBean attribute. The simple task left for you is to connect them in an xml configuration file.
  
IOC and spring
  
IOC is a design pattern that externalizes application logic, so it is injected rather than written into client code. The combination of IOC and interface programming applications produces an architecture like the spring framework, which can reduce the client's dependency on specific implementation logic. For more information about IOC and spring, see references.
  
Dependency injection is a powerful feature, but the Spring framework can provide more features. Spring supports pluggable transaction managers to provide a wider range of options for transaction processing. It integrates a leading persistence framework and provides a consistent exception hierarchy. Spring also provides a simple mechanism to replace normal object-oriented code with Aspect-Oriented code.
  
Spring AOP allows you to use an interceptor to intercept application logic on one or more execution points. Enhancing the logging logic of applications in the interceptor will generate a more readable and practical code base, so the interceptor is widely used for logging. You will soon see that Spring AOP has released its own Interceptor to deal with cross-cutting concerns. You can also write your own interceptor.
  
Integrate Struts and spring
  
Similar to struts, spring can be implemented as an MVC. Both 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 building high-quality software when time is tight. Struts is so powerful that the development team prefers to integrate the features of the Spring framework rather than converting it into spring MVC. There is no need to switch. This is good news for you. The spring architecture allows you to connect struts as a Web framework to the spring-based business and persistence layer. The final result is that all conditions are ready.
  
In the following tips, you will learn three methods to integrate Struts MVC into the spring framework. I will reveal the defects of each method and compare their advantages. Once you understand the role of all three methods, I will show you an exciting application that I like most of the three methods.
  
Three Tips
  
Each of the following Integrated Technologies (or TIPS) have their own advantages and features. I prefer one of them, but I know these three methods can deepen your understanding of struts and spring. When dealing with different situations, this will provide you with a broad range of options. The method is as follows:
  
Integrate structs with the actionsupport class of spring
  
Use spring's delegatingrequestprocessor to overwrite struts's requestprocessor
  
Delegate struts Action Management to Spring framework
  
Load application environment
  
Regardless of the technology you use, you must use spring's contextloaderplugin to load the spring application environment for the struts actionservlet. Just like adding any other plug-in, simply add the plug-in to your struts-config.xml file, as shown below:
  
<Plug-in classname =
"Org. springframework. Web. Struts. contextloaderplugin">
<Set-Property =
"Contextconfiglocation" value = "/WEB-INF/beans. xml"/>
</Plug-in>
  
As mentioned above, in the download section, you can find the complete source code of the three fully usable examples. Each example provides a different struts and spring Integration Method for a book search application. You can see the main points of the example here, but you can also download the application to view all the details.
  
Tip 1. Use spring actionsupport
  
Creating a spring environment manually is the most intuitive way to integrate Struts and spring. Spring provides some help to make it easier. The Org. springframework. Web. Struts. actionsupport class provides a getwebapplicationcontext () method to conveniently obtain the spring environment. All you do is expand your actions from the spring actionsupport instead of the struts action class, as shown in Listing 1:
  
List 1. integrate Struts with actionsupport
  
Package ca. nexcel. Books. actions;
  
Import java. Io. ioexception;
  
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
  
Import org. Apache. Struts. Action. actionerror;
Import org. Apache. Struts. Action. actionerrors;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;
Import org. Apache. Struts. Action. dynaactionform;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Web. Struts. actionsupport;
  
Import ca. nexcel. Books. Beans. Book;
Import ca. nexcel. Books. Business. bookservice;
  
Public class searchsubmit extends actionsupport {| (1)
  
Public actionforward execute (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response)
Throws ioexception, servletexception {
  
Dynaactionform searchform = (dynaactionform) form;
String ISBN = (string) searchform. Get ("ISBN ");
  
// The old fashion way
// Bookservice = new bookserviceimpl ();
  
Applicationcontext CTX =
Getwebapplicationcontext (); | (2)
Bookservice =
(Bookservice) CTX. getbean ("bookservice"); | (3)
  
Book = bookservice. Read (ISBN. Trim ());
  
If (null = book ){
Actionerrors errors = new actionerrors ();
Errors. Add (actionerrors. global_error, new actionerror
("Message. notfound "));
Saveerrors (request, errors );
Return Mapping. findforward ("failure ");
}
  
Request. setattribute ("book", book );
Return Mapping. findforward ("success ");
}
}
  
Let's take a quick look at what happened here. In (1), I created a new action by extending from the actionsupport class of spring instead of the action class of struts. At (2), I use the getwebapplicationcontext () method to obtain an applicationcontext. To obtain business services, I used the environment obtained in (2) to find a spring bean in (3.
  
This technology is simple and easy to understand. Unfortunately, it coupling the struts action with the Spring framework. If you want to replace spring, you must rewrite the code. In addition, because the struts action is not controlled by spring, it cannot gain the advantage of Spring AOP. This technology may be useful when multiple independent spring environments are used, but in most cases this method is not as suitable as the other two methods.
  
Tip 2. Overwrite requestprocessor
  
Separating spring from struts actions is a more clever design choice. One way to separate is to use the org. springframework. Web. Struts. delegatingrequestprocessor class to overwrite the struts requestprocessor handler, as shown in Listing 2:
  
Listing 2. Integration Using spring's delegatingrequestprocessor
  
<? XML version = "1.0" encoding = "ISO-8859-1"?>
  
<! Doctype Struts-config public
"-// Apache Software Foundation // DTD struts configuration 1.1 // en"
Http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd>
  
<Struts-config>
<Form-beans>
<Form-bean name = "searchform"
Type = "org. Apache. Struts. validator. dynavalidatorform">
<Form-property name = "ISBN" type = "Java. Lang. String"/>
</Form-bean> <br <TD>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.