第一步:從struts的jar包中struts-spring.jar包導入項目中。
TestAction.java內容:
public class TestAction {
private TestService testService;
public void setTestService(TestService testService) {
this.testService = testService;
}
public String execute(){
System.out.println(testService.validate());
return "test";
}
}
第二步:修改struts.xml內容
<package name="default" namespace="/" extends="struts-default">
<action name="welcom">
<result>/WEB-INF/MyJsp.jsp</result>
</action>
<action name="testAction" class="testAction">
<result name="test">/WEB-INF/MyJsp.jsp</result>
</action>
</package>
第三步:修改applicationContext.xml內容
<bean id="testAction" class="com.dx.mobile_scm.test.TestAction">
<property name="testService" ref="testService" />
</bean>
<bean id="testService" class="com.dx.mobile_scm.service.TestServiceImpl"></bean>
注意:由於spring中默認由名稱裝配,所以Action可以不需要再spring中配置,但其中的服務名稱(testService)必須與
spring配置文件中的bean的id相同,這種方法不推薦。