Integrate Spring with Struts

Source: Internet
Author: User

The Spring framework is an excellent lightweight Java EE container, and the Spring framework is the core of the entire lightweight Java EE Framework. For most Java EE applications, Spring containers are used to manage components in applications to ensure low coupling between components. The Struts2 framework is only a MVC framework and a Web layer solution. It integrates with the Spring framework through a plug-in that is "pluggable.

Struts2 provides two basic integration policies, one of which is to hand over the Action instance to the Spring container for generation and management. In this way, the IoC features of Spring containers can be fully utilized, provides the best decoupling. Another policy is to use the automatic assembly method of Spring plug-ins. When the Spring plug-in creates an Action instance, it immediately injects the corresponding business logic component in the Spring container into the Action instance. These two methods allow the Struts2 Action to access the Bean in the Spring container. The key to integration is to allow Struts2 actions to access the business logic components in the Spring container, no matter which of the above two methods, allows Action to access the business logic components in the Spring container.

I. Struts2 plug-in

The Struts2 plug-in is a JAR file that can be used to expand, change, or add Struts2 functions. You only need to copy the jar file to the web application.

The Struts2 plug-in Jar files must all contain a configuration file named struts-plugin.xml, and the rest of the common struts. xml files have the same content. The struts2 plug-in is loaded in random order, but the configuration file is indeed loaded in the order of struts-default.xml, struts-plugin.xml, struts. xml. From the perspective of system implementation, if we need to extend the struts2 function, we can implement it by providing a new interceptor and a new Bean instance. From the perspective of system structure, if we need to extend the struts2 function, we can simply provide a new plug-in.

Spring and struts2 integration, must install the spring plug-in struts2, just copy the struts2-spring-plugin-x.x.x.x.jar file to the Web application WEB-INF/lib path. Once the spring plug-in is installed, we can control all the actions through spring, or use spring injection after struts2 is created. To use the spring container, We need to initialize the spring container first. spring provides the ContextLoaderListener class, which can be used as the Listener of the Web application, it will automatically look up applicationContext in the WEB-INF path at Web application startup. xml configuration file, and create a Spring container based on this file. If you use another configuration file or another configuration file, you can use the contextConfigLocation parameter to specify it, as shown below:

 

ContextConfigLocation
/WEB-INF/xxxDao. xml,/WEB-INF/applicationContext. xml


Org. springframework. web. context. ContextLoaderListener

Next, let's let the spring container manage the controllers in the application. The core controller of struts2 first intercepts user requests and then forwards the requests to corresponding actions for processing. This process is fixed. But in fact, the Action instance is generated by the struts2 container. We need to use the spring container to generate it and make full use of the ioc feature of spring. The spring plug-in provides an Action. when the xml file is used with the child action, the specified class points to the BeanID in the spring container. The Struts2 action is just a decoration, and the spring container is the real controller.

Configure struts2.xml as follows:



/WEB-INF/jsp/error. jsp
/WEB-INF/jsp/welcome. jsp

 

This loginAction class is not the actual processing class. We need to configure this Bean in applicationContext. xml, as shown below:

If other beans are used, injection can also be performed on the bean. In this way, the spring container is implemented to generate actions, but it is clear that the action configuration is bloated and the code readability is reduced.

Ii. Use automatic assembly

Under this policy, the action is still created by the spring plug-in, but the automatic assembly policy of spring is used. If no Assembly policy is specified, automatic assembly is performed by name by default. The Automatic Assembly policy of the spring plug-in is specified through the struts. objectFactory. spring. autoWire constant. This constant can have several types of constants: name, type, auto, and constructor.

Modify the struts. xml configuration as follows:



/WEB-INF/jsp/error. jsp
/WEB-INF/jsp/welcome. jsp

 

It can be seen that there is no difference between spring and ing. When configuring an action, you must specify the implementation class of the action. In this case, the dependency object or business logic object in the action Implementation class is different, we can inject it through spring.

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.