搭建Struts2+Spring+Hibernate

一、先在项目中导入strut2

  需要用到的包:核心包和strut2-spring包。

二、再导入spring

  我使用的是spring3.0;需要用的包:核心包、AOP、JDBC、WEB包。

三、在web.xml中配置spring

  <context-param>

  <!--注意大小写-->
    <param-name>contextConfigLocation</param-name>

  <!--我的applicationContext.xml文件在源文件根目录下;所有我使用classpath设置;如果该文件在其他位置请加上路径-->
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener> 

四、编写Action类

  最好写一个BaseAction类;然后所有的Action都继承该类; BaseAction类如下:

  import com.opensymphony.xwork2.ActionSupport;

  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;

  import org.apache.struts2.ServletActionContext;

  /**
   * 总的Action类,所有Action继承,获得request、response、sessio
   */
 public class BaseAction extends ActionSupport {
 
   private HttpServletRequest request;
   private HttpServletResponse response;
   private HttpSession session;

   public HttpServletRequest getRequest() {
      return ServletActionContext.getRequest();
   }

   public void setRequest(HttpServletRequest request) {
      this.request = request;
   }

   public HttpServletResponse getResponse() {
      return ServletActionContext.getResponse();
   }

   public void setResponse(HttpServletResponse response) {
      this.response = response;
   }

   public HttpSession getSession() {
      return ServletActionContext.getRequest().getSession();
   }

   public void setSession(HttpSession session) {
      this.session = session;
   }
 }

五、导入Hibernate

  将所有的包都勾上;specify database connection details选项的钩去掉;create SessionFactory class选项的钩同样去掉;Finish;出现一个包冲突的警告;不用理它;keep existing;

六、分别配置struts.xml、applicationContext.xml、hibernate.cfg.xml文件

  最主要是spring的applicationContext.xml文件的配置;示例:

  <?xml version="1.0" encoding="UTF-8"?>
  <beans
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
   ">

     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />

    </bean>
     <!-- 声明式事务 -->
     <!-- aop 注入事务 -->
     <!--定义Hibernate的事务管理器HibernateTransactionManager -->
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <!-- 依赖注入上面定义的sessionFactory -->  
        <property name="sessionFactory" ref="sessionFactory" />
     </bean>
     <!-- advice 通知-->
     <tx:advice id="txAdvice" transaction-manager="transactionManager" />

      <tx:attributes>
         <!-- 以下列名称开头的所有方法均进行事务控制,如果当前没有事务,则新建一个事务 -->  
         <tx:method name="add*" propagation="REQUIRED"/>
         <tx:method name="up*" propagation="REQUIRED"/>
         <tx:method name="del*" propagation="REQUIRED"/>
          <!-- 以下列开头的所有方法采用只读型事务控制类型,如果有事务则用,没有则不用 -->   
         <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
       </tx:attributes>
     </tx:advice>
     <!-- pointcut 切入点 -->
     <aop:config>  
        <aop:pointcut expression="execution(* com.ssh.biz.*.*(..))" id="bizTransaction"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="bizTransaction"/>
     </aop:config>
     <!-- dao -->
     <bean id="hibernateDao" class="com.ssh.dao.imp.HibernateDaoImp">
        <property name="sessionFactory" ref="sessionFactory" />
     </bean>
     <!-- biz -->
     <bean id="userBiz" class="com.ssh.biz.imp.UserBizImp">
        <property name="hibernateDao" ref="hibernateDao"></property>
     </bean>
     <!-- action -->
     <bean id="userAction" class="com.ssh.action.UserAction">
        <property name="userBizImp" ref="userBiz"></property>
     </bean>
   </beans> 

  其他两个文件的内容基本很少;就不写了.如果运行项目不报错;那么这次ssh框架的配置就算是成功了! 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值