Versions Compared

Key

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

...

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.

Code Block
JAVA
JAVA
1title Set Method For EditService Object


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.

Code Block
XML
XML
1Spring Listener In 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