ssh集成的三种方法


struts1.2版本与spring集成
spring+struts的集成(第一种方案)
原理:在Action中取得BeanFactory,然后通过BeanFactory获取业务对象

1、spring和struts集成
 * struts的配置
  --拷贝struts类库和jstl类库
  --在web.xml文件中配置ActionServlet
  --提供struts-config.xml文件
  --提供国际化资源文件
 * spring的配置
  --拷贝spring类库 
  --提供spring配置文件 
2、在struts的Action中调用如下代码取得BeanFactory
 BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
 
3、通过BeanFactory取得业务逻辑对象,调用业务逻辑方法
-------------------------
 <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
 
  <context-param>
   <param-name>contextConfigLocation</param-name>
 <!--
   <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext-*.xml</param-value>
    -->
    <param-value>classpath*:applicationContext-*.xml</param-value>
  </context-param>
----------------------------
方法二:
spring+struts的集成(第二种方案)
原理:将业务逻辑对象通过spring注入到Action对象中,从而避免了在Action中直接代码查找

1、spring和struts集成
 * struts的配置
  --拷贝struts类库和jstl类库
  --在web.xml文件中配置ActionServlet
  --提供struts-config.xml文件
  --提供国际化资源文件
 * spring的配置
  --拷贝spring类库 
  --提供spring配置文件
  
2、因为我们在Action中需要调用业务逻辑方法,所以需要定义setter方法,让spring将业务逻辑对象注入过来

3、在struts-config.xml文件中配置Action
  * <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy
    DelegatingActionProxy代理类主要是获取BeanFactory,然后根据<action>标签中的path属性值,到IoC容器中
    查找本次请求的Action
4、在spring中的配置如下:
      <bean name="/login" class="com.hdaccp.usermgr.web.actions.LoginAction">
         <property name="userManager" ref="userManager"/>
      </bean>
      * <bean>标签中必须使用name属性,name属性值必须等于struts-config.xml文件中<action>标签中的path属性值
      * 必须注入业务逻辑对象
      * 建议将<bean>标签中scope设置为prototype,这样就避免了struts Action的线程安全问题 

第三种方案:
使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖struts的高层处理类RequestProcessor,这就要在struts-config.xml中用controller声明:
 .....................
 <action-mappings>
  ...........
  <action path="/login"
   type="struts.action.LoginAction"
   name="loginForm">
  <forward name="success" path="/WEB-INF/main.jsp">
  </action>
  ...........
 </action-mappings>
 .....................
 <controller processorClass="org.springframework.web.struts.DelegatinRequestProcessor"/>
 .....................
 
另外,还要在spring配置文件中,声明对应某个action的bean,而且bean的name值必须和action的path一致,因为spring容器是根据此bean的name来找到相应的action,然后再进行相应的注入操作的:
 ................
 <bean id="loginService" class="business.LoginServiceImpl">
         <property name="name" value="aaaaa"/>
     </bean>

     <bean name="/login"
      class="struts.action.LoginAction">
         <property name="loginService">
             <ref bean="loginService"/>
         </property>
     </bean>
 ................

猜你喜欢

转载自bao1073740756-126-com.iteye.com/blog/1487065