首先要在web.xml文件中添加监听器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath前缀指定从类路径下寻找-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
然后在applicationcontext.xml中配置要管理的action:
<bean id="loginAction" class="com.zmj.action.LoginAction">
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
需要注意的是employeeServiceInter为loginAtion中的属性:
LoginAction.java:
private EmployeeServiceInter employeeServiceInter;
public void setEmployeeServiceInter(EmployeeServiceInter employeeServiceInter) {
<span style="white-space:pre"> </span>System.out.println("调用");
<span style="white-space:pre"> </span>this.employeeServiceInter = employeeServiceInter;
<span style="white-space:pre"> </span>}
而ref="employeeService"中的employeeService为已经配置了的bean:
<bean name="employeeService" class="com.zmj.service.imp.EmployeeService">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
最后在struts2.xml中配置
<constant name="struts2.ObjectFactory" value="Spring"/>
再配置action:
<package name="front" namespace="/login" extends="struts-default">
<action name="login" class="loginAction">
<result name="success" type="chain">
showMessage
</result>
<result name="error" type="dispatcher">
<param name="location">/index.jsp</param>
<param name="error">error</param>
</result>
</action>
</package>
需要注意的是此时action中的class属性对应的class为在applicationcontext.xml中配置了的bean:
<bean id="loginAction" class="com.zmj.action.LoginAction">
<property name="employeeServiceInter" ref="employeeService"/>
</bean>
中的id。
到这里就已经配置完毕啦。