基于MyEclipse6.5的SSH整合

本文介绍了两种Spring与Struts的集成方式,一种是在Action中手动获取BeanFactory,另一种是通过Spring自动注入业务逻辑对象到Action中。同时,还提供了SSH(Spring+Struts+Hibernate)集成的web.xml配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

借鉴下网易“我是Y头”:

http://blog.163.com/duoluo_tian_shi/blog/static/651747602009485553566/

 

包下载:

http://www.jarbao.com/index.htm

 

Spring+struts 集成方案一

1. web.xml

   <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath*:applicationContext-*.xml</param-value>

  </context-param>

  

    <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

2、在strutsAction中调用如下代码取得BeanFactory

     BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());

 

3、通过BeanFactory取得业务对象,调用业务逻辑方法

 

     UserManager userManager = (UserManager)factory.getBean("userManager");   

spring+struts的集成(第二种集成方案)

 

原理:将业务逻辑对象通过spring注入到Action中,从而避免了在Action类中的直接代码查询

 

 

 

 

1. web.xml

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath*:applicationContext-*.xml</param-value>

  </context-param>

  

    <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

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

public class LoginAction extends Action {

 

    private UserManager userManager;

   

    public ActionForward execute(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           ……

    }

    public void setUserManager(UserManager userManager) {

       this.userManager = userManager;

    }

}

3、在struts-config.xml文件中配置Action

     * <action>标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy

      DelegatingActionProxy是一个Action,主要作用是取得BeanFactory,然后根据<action>中的path属性值到IoC容器中取得本次请求对应的Action

      <action path="/login"

       type="org.springframework.web.struts.DelegatingActionProxy"

              name="loginForm"

              scope="request"  

       >

           <forward name="success" path="/success.jsp"/>

       </action>

4、在spring配置文件中需要定义strutsAction,如:

     * 必须使用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>

 

 

SSH集成 web.xml

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath*:applicationContext-*.xml</param-value>

  </context-param>

  

   <listener>

       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

  

  <filter>

    <filter-name>Spring character encoding filter</filter-name>

    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

    <init-param>

    <param-name>encoding</param-name>

    <param-value>GBK</param-value>

    </init-param>

  </filter>

 

  <filter-mapping>

    <filter-name>Spring character encoding filter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

  <filter>

    <filter-name>hibernateFilter</filter-name>

    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

  </filter>

 <filter-mapping>

    <filter-name>hibernateFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值