Info |
---|
The example code for this tutorial, spring_struts, is available for checkout at https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examples/ |
Introduction
...
If you examine the example application for the Struts 2 Themes tutorial you'll see this code in the EditAction ActionSupport class
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
private EditService editService ; |
...
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
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 | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<?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 | ||||||
---|---|---|---|---|---|---|
| ||||||
<?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" > <property name="editService" ref="editService" /> </bean> </beans> |
...
In the struts.xml 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 | |||||||
---|---|---|---|---|---|---|---|
| |||||||
<action name="edit" class="editAction" method="input"> <result name="input">/edit.jsp</result> </action> |
...