Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

The example code for this tutorial, Spring_Struts2_Ant or Spring_Struts2_Mvnspring_struts, is available on Google Code - httpfor checkout at https://codegithub.google.com/papache/struts2-examples/downloads/listImage Removed. After downloading and unzipping the file, you'll have a folder named Spring_Struts2_Ant (or Spring_Struts2_Mvn). In that folder will be a README.txt file with instructions on now to build and run the example application.struts-examples

Introduction

In the execute method of many Struts 2 ActionSupport classes are statements that create objects and then have those objects execute methods that perform needed tasks. Whenever one class creates an object of another class that introduces a dependency between the two classes. The Spring framework makes it easier for the application developer to manage these dependencies and helps makes make the application more flexible and maintainable. This tutorial will show you how to use Struts 2 and Spring together to manage the dependencies between your ActionSupport classes and other classes in your application.

...

If you examine the example application for the Struts 2 Themes tutorial you'll see this code in the EditAction ActionSupport class

Code Block
JAVAjavaJAVA
java
titleEditAction Class Hard-Coded Dependency


private EditService editService = new EditServiceInMemory();

...

Spring provides a mechanism to manage dependencies by injecting them at run time. Struts 2 ActionSupport classes—like any other Java class—can be injected with a dependent object by the Spring framework. So instead of having the above code, I would have this statement in EditAction.

Code Block
JAVAjavaJAVA
java
titleEditAction Class No Hard-Coded Dependency


    private EditService editService ;

...

For a Maven application you'll need to add a dependency to the struts2-spring-plugin jar (see the Maven example application for this tutorial). The plugin's pom.xml includes transitive dependencies to the Spring jar files. For an Ant built application you'll need to add the struts2-spring-plugin jar and the Spring jars to your application's class path (see the Ant example application for this tutorial).

Tip

The current version (2.3.15) of the Struts 2 Spring plugin has transitive dependencies to the Spring 23.0.5 .6 version. If you want to use the latest version of Spring, then you should exclude the transitive dependencies in your pom.xml for the Struts 2 Spring plugin and then declare dependency nodes to the current version of the Spring jar files. If you are using Ant and explicitly including the jar files in your application, then just include the latest version of the Spring jar files.

In your ActionSupport class you must have a set method for the dependent object that follows standard Java bean naming conventions. If you examine the EditAction class for this tutorial's application you'll see this set method.

Code Block
JAVAjavaJAVA
java
1title Set Method For EditService Object


public void setEditService(EditService editService) {
		
   this.editService = editService;
		
}

...

To make our application "Spring aware" we need to add this line to web.xml.

Code Block
XMLxmlXML
xml
titleSpring Listener In web.xml


<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

...

In the Spring configuration file we create a bean node for those objects we want Spring to create an instance of and inject into our ActionSupport class. In the example application is this applicationContext.xml.

Code Block
XMLxmlXML
xml
titleSpring Configuration File


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="editService" class="org.apache.struts.edit.service.EditServiceInMemory" />

</beans>

...

Using the above methodology, the Struts 2 framework will still manage the creation of the ActionSupport class. If you prefer you can configure the application so that Spring will create the ActionSupport class also. To support this technique you need to add a bean node to the Spring configuration file for the ActionSupport class.

Code Block
XMLxmlXML
xml
titleSpring Configuration For ActionSupport Class Managed By Spring


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
            

<bean id="editService" class="org.apache.struts.edit.service.EditServiceInMemory" />

<bean id="editAction" class="org.apache.struts.edit.action.EditAction" scope="prototype">

	<property name="editService" ref="editService" />
	
</bean>

</beans>

Note in the above that there is an editAction bean  bean and its editService property  property is set to the editService bean bean. Since we are having Spring manage the EditAction class  class we must specify any properties of EditAction that  that we want Spring to inject. Please remember that actions must be created on each request, they cannot be singletons - this is the default scope that's why it must be changed to prototype.

In the struts.xml configuration  configuration file you must specify the Spring id value for the class attribute of the action node. This tells Struts to get a bean with that id value from Spring for the Action class.

Code Block
XMLxmlXML
xml
titleStruts Configuration For Spring Managed ActionSupport Class


<action name="edit" class="editAction" method="input">
	<result name="input">/edit.jsp</result>
</action>

...

In this tutorial we reviewed how to use the Struts 2 Spring plugin to integrate Spring and Struts. By using the Struts 2 Spring plugin you can have Spring manage the dependencies of your ActionSupport classes. Of course you can also take advantage of the many other benefits (AOP, Spring JDBC) that the Spring framework provides.

Next

Onward to Struts 2 Annotations

Previous

Return to Struts 2 Themes