spring+struts的集成(第二种集成方案,用得最多的方案)

来源:互联网 发布:穆雅斓淘宝店 编辑:程序博客网 时间:2024/05/19 06:19

 

注意三点:

1、因为Action需要调用业务逻辑方法,所以需要在Action中提供setter方法,让spring将业务逻辑对象注入过来

 private UserManager userManager;

 public void setUserManager(UserManager userManager) {
  this.userManager = userManager;
 }

 

2、在struts-config.xml文件中配置Action
  * <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy
   DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值
   到IoC容器中取得本次请求对应的Action
   <struts-config>
 <form-beans>
  <form-bean name="loginForm" type="com.bjsxt.usermgr.forms.LoginActionForm"/>
 </form-beans>
 
 <action-mappings>
  
  <action path="/logininput"
    forward="/login.jsp"
  ></action>
 
  <action path="/login"
    type="org.springframework.web.struts.DelegatingActionProxy"
    name="loginForm"
    scope="request" 
  >
   <forward name="success" path="/success.jsp"/>
  </action>
 </action-mappings>

    <message-resources parameter="MessageResources" />
</struts-config>
3、在spring配置文件中需要定义struts的Action,如:
 <bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">
  <property name="userManager" ref="userManager"/>
 </bean>
 * 必须使用name属性,name属性值必须和struts-config.xml文件中<action>标签的path属性值一致
 * 必须注入业务逻辑对象
 * 建议将scope设置为prototype,这样就避免了struts Action的线程安全问题

 

 <bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">
  <property name="userManager" ref="userManager"/>
 </bean>

原创粉丝点击