Struts+Hibernate+Spring 配置

本文详细介绍了JavaWeb开发中使用Spring、Hibernate和Struts框架进行整合配置的方法,包括struts1的web.xml配置、Spring与hibernate的配置,以及如何在三个spring文件中细分配置,以实现更清晰的操作,以帮助开发者更好地理解和应用这些框架。

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

java web 开发中常用的 框架 就是 ,struts 1 ,hibernate ,spring 整合,
在这里把它贴出来,经常忘记,加深一下印象。

首先配置 基本的 struts 1
web.xml 配置方式如下,先加上这个配置:

<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>
    </servlet>
  
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
下面是spring 与 hibernate,
这个与hibernate 配置 ,细分成了 三个spring文件,看起来更加清晰。
先在web.xml 加入这个配置:

    <listener>
        <listener-class>            
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/spring-basic-Context.xml,
            /WEB-INF/config/spring-dao-Context.xml,
            /WEB-INF/config/spring-services-Context.xml    
        </param-value>
    </context-param>

下面分别是 三个spring 配置,,简单的book操作,做个例子。

<!-- spring-basic-Context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/config/db.properties</value>
            </list>
        </property>
    </bean>


    <bean id="c3P0dataSource"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${DB.DRIVER}" />
        <property name="jdbcUrl" value="${DB.URL}" />
        <property name="user" value="${DB.USERNAME}" />
        <property name="password" value="${DB.PASSWORD}" />
        <property name="initialPoolSize" value="10" />
        <property name="maxPoolSize" value="30" />
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="c3P0dataSource" />
        </property>
     
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.jdbc.batch_size">25</prop>
            </props>
        </property>


        <property name="mappingResources">
            <list>
                <value>com/birds/book/bean/Book.hbm.xml</value>
            </list>
        </property>
    </bean>

 


    <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
 <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager" ref="myTxManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED,-com.birds.book.common.BookException</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>


    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <value>*Services</value>
        </property>
      
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>


</beans>

 

<!-- spring-dao-Context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
  >
    <bean id="bookDao" class="com.birds.book.dao.impl.BookDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

<!-- spring-services-Context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    >
    <bean id="bookServices" class="com.birds.book.services.impl.BookServicesImpl">
       <property name="bookDaoImpl" ref="bookDao" />
    </bean>
</beans>
         
<!-- spring-action-Context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
    >
    <bean name="/book" class="com.birds.view.action.BookAction" scope="prototype">
        <property name="bookServicesImpl" ref="bookServices" />
    </bean>
</beans>
      
<!-- struts-config.xml 配置-->

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
    <data-sources />
    <form-beans>


        <form-bean name="bookForm" type="com.birds.view.form.BookForm">        
        </form-bean>
    </form-beans>
    <global-exceptions />
    <global-forwards />
    <action-mappings>
        <action path="/book"
        name="bookForm"
        type="org.springframework.web.struts.DelegatingActionProxy"
        scope="request">
            <forward name="success" path="/book.jsp" />
        </action>
    </action-mappings> 


    <plug-in
        className="org.springframework.web.struts.ContextLoaderPlugIn">
        <set-property property="contextConfigLocation"
            value="/WEB-INF/config/spring-action-Context.xml" />
    </plug-in>
</struts-config>

 

 

     以上配置完,都是依赖注入。 这些框架的配置 都差不多。主要还是根据实际的业务来 加其他的组件进去。

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/birdsaction/archive/2008/12/14/3511018.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值