You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

The example code for this tutorial, Spring_Struts2_Ant or Spring_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. 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.

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 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.

This tutorial assumes you understand how to use the Spring framework to manage dependencies between classes. You can learn more about Spring by reading the documentation at http://www.springsource.org/documentation

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.

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

EditAction Class Hard-Coded Dependency

private EditService editService = new EditServiceInMemory();

The above statement hard-codes a dependency between the EditAction class and the EditServiceInMemory class. This is poor design for two reasons.

  1. If I need to replace the EditServiceInMemory with another class that implements the EditService interface I'll have to hunt down and replace all statements where I hard-coded the dependency.
  2. I cannot test EditAction without using the EditServiceInMemory class. I cannot isolate EditAction by using a stub implementation of EditService when writing my test case because the use of EditServiceInMemory is hard-coded.

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.

EditAction Class Hard-Coded Dependency

    private EditService editService ;

At run time the Spring framework will provide an object of a class that implements the EditService interface.

Struts 2 Spring Plugin

Struts 2 provides a plugin that enables Spring to inject into the ActionSupport classes any dependent objects you've specified in the Spring configuration file. Consult Spring Plugin for more information about how the plugin works.

For a Maven application you'll need to a dependency to the struts2-spring-plugin jar (see the Maven example application for this tutorial). For a 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).

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.


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

Spring will use that set method to provide an object of type EditService to the EditAction class at run time.

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


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

The above code will activate the Spring framework when the application is started up by the Servlet container. By default Spring will look for a configuration file name applicationContext.xml in WEB-INF (consult the Spring documentation for how you can change where Spring looks and what the configuration file name is).

Spring Configuration File

  • No labels