Struts+Hibernate+Spring 配置

来源:互联网 发布:手机声音增大软件 编辑:程序博客网 时间:2024/03/29 03:33

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>

 

 

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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/birdsaction/archive/2008/12/14/3511018.aspx

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅蓝note5很卡怎么办 魅蓝note5锁了怎么办 荣耀9开不开机怎么办 4s更新后用不了怎么办 魅蓝3开不了机怎么办 手机不支持联通4g网络怎么办 华为手机出现emui界面怎么办 华为畅玩4x内存不足怎么办 手机电源键掉了怎么办 手机电源键坏了怎么办 小米5s听筒声音小怎么办 荣耀8电源键失灵怎么办 华为荣耀3c卡怎么办 大王卡是2g网络怎么办 联通停用2g副卡怎么办 华为荣耀8忘记解锁密码怎么办 华为手机内存满了怎么办 华为手机无限重启怎么办 华为3c重启怎么办 荣耀6 无限重启怎么办 手机进水无法开机了怎么办 华为手机不停重启怎么办 华为手机反复重启怎么办 酷派电池不耐用怎么办 美图手机充电慢怎么办 酷派b770太卡怎么办 酷派手机出现无命令怎么办 华为荣耀4x卡怎么办 华为手机图案解锁忘了怎么办 xp电脑读不起u盘怎么办 在外国玩王者卡怎么办 华为p7忘记解锁密码怎么办 华为荣耀4x存储空间不足怎么办 红米4a内存不够怎么办 华为h60开不了机怎么办 华为荣耀4c内存不足怎么办 华为4c运行内存不足怎么办 华为手机总是显示内存不足怎么办 华为荣耀4x畅玩版内存不足怎么办 三星手机忘了解锁密码怎么办 荣耀8密码忘了怎么办