ssh配置详解

本文介绍了如何在Struts框架中整合Spring框架,包括在web.xml文件中的配置、Struts配置以及Spring配置文件的详细设置。通过这些步骤,可以实现数据库连接池、Hibernate会话工厂的初始化,并对事务进行管理。

一.首先配置web.xml

<!--加入spring 配置-->

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 利用spring监听 编码设置-->
<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>utf-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>Spring character encoding filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配上openSessionInViewFilter ( 作用hibernate加载问题,可解决session close..)-->

<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>*.do</url-pattern>
</filter-mapping>

<!--加入struts的配置-->

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

--------------------------------

二在struts中配置

<!--加入Spring代理-->

<message-resources parameter="com.lch.struts.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>

<!--在action中代理-->

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

三spring

<?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:tx="
http://www.springframework.org/schema/tx "
    xmlns:aop="
http://www.springframework.org/schema/aop "
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
" default-lazy-init="true">

<bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource">
   <property name="driverClassName"
    value="com.mysql.jdbc.Driver">
   </property>
   <property name="url"
    value="jdbc:mysql://localhost:3306/china">
   </property>
   <property name="username" value="root"></property>
   <property name="password" value="root"></property>
</bean>

<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="dataSource"
    ref="dataSource" >
   </property>
   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">
      org.hibernate.dialect.MySQLDialect
     </prop>
     <prop key="hibernate.show_sql">true</prop>
    </props>
   </property>
   <property name="mappingResources">
    <list>
     <value>com/lch/bbs/dao/Message.hbm.xml</value>
     <value>com/lch/bbs/dao/Replay.hbm.xml</value>
     <value>com/lch/bbs/dao/User.hbm.xml</value>
     </list>
   </property>

<!--再加入事务管理-->

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
     <ref bean="sessionFactory"/>
   </property>
</bean>

<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
   <property name="transactionManager">
        <ref bean="transactionManager"/>  
   </property>
   <property name="transactionAttributes">
     <props>
       <prop key="save*">PROPAGATION_REQUIRED,-LogicException</prop>
       <prop key="delete*">PROPAGATION_REQUIRED,-LogicException</prop>
       <prop key="update*">PROPAGATION_REQUIRED,-LogicException</prop>
       <prop key="find*">PROPAGATION_REQUIRED,-LogicException</prop>
       <prop key="get*">PROPAGATION_REQUIRED,-LogicException</prop>
       <prop key="list*">PROPAGATION_REQUIRED,-LogicException,readOnly</prop>
     </props>
   </property>
</bean>

<bean id="logAdvice" class="com.example.exception.LogExceptionAdvice"/>
<bean id="autoAppendLogAdvice" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
      <property name="beanNames">

<!--配置id=*ManagerTarget-->
        <list><value>*ManagerTarget</value></list>
      </property>
      <property name="interceptorNames">

<!--logAdvice就是上面的ID class="com.example.exception.LogExceptionAdvice"-->
        <value>logAdvice</value>
      </property>
    </bean>

<bean id="usersDao" class="com.example.gw.users.UsersDao">
      <property name="sessionFactory">
        <ref bean="sessionFactory"/>
      </property>
    </bean>

<bean id="usersManagerTarget" class="com.example.gw.users.UsersManager">
      <property name="usersDao">
        <ref bean="usersDao"/>
      </property>
    </bean>
</bean>

转自http://blog.youkuaiyun.com/kaikai19871010/archive/2009/08/21/4469322.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值