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 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.
...
Code Block |
---|
| java |
---|
title | EditAction Class Hard-Coded Dependency |
---|
| java |
---|
|
private EditService editService = new EditServiceInMemory();
|
...
Code Block |
---|
| java |
---|
title | EditAction Class No Hard-Coded Dependency |
---|
| java |
---|
|
private EditService editService ;
|
...
Code Block |
---|
| java |
---|
1 | title Set Method For EditService Object |
---|
| java |
---|
|
public void setEditService(EditService editService) {
this.editService = editService;
}
|
...
Code Block |
---|
| xml |
---|
title | Spring Listener In web.xml |
---|
| xml |
---|
|
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
|
...
Code Block |
---|
| xml |
---|
title | Spring Configuration File |
---|
| xml |
---|
|
<?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>
|
...
Code Block |
---|
| xml |
---|
title | Spring Configuration For ActionSupport Class Managed By Spring |
---|
| xml |
---|
|
<?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>
|
...
Code Block |
---|
| xml |
---|
title | Struts Configuration For Spring Managed ActionSupport Class |
---|
| xml |
---|
|
<action name="edit" class="editAction" method="input">
<result name="input">/edit.jsp</result>
</action>
|
...