SSH整合

本文详细介绍了SSH(Spring、Hibernate、Struts2)的整合过程,包括Spring与Hibernate的配置,如加载db.properties、配置dataSource、sessionFactory及DAO和服务层;接着是Spring与Struts2的整合,涉及到Struts2核心过滤器的配置以及struts.xml的添加和配置。

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

一、Spring+Hibernate

1、拷贝jar包

这里写图片描述

2、配置web.xml

2.1、加载applicationContext.xml
<!-- 加载applicationContext.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.2、添加和配置applicationContext.xml

首先在配置之前,我们需要数据库连接,先创建一个db.properties的资源文件。

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/admindb
jdbc.username=root
jdbc.password=123

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update

配置applicationContext.xml
applicationContext.xml的配置循序为:
db.properties–>dataSource–>sessionFactory–>xxxDao–>xxxService–>xxxAction

<!-- 加载db.properties -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源dataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}"
        p:username="${jdbc.username}"
        p:password="${jdbc.password}"
        />

    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:dataSource-ref="dataSource"
    >
        <!-- 加载hibernate的配置,方言,等信息 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>

        <!-- 引入映射文件 -->
        <property name="mappingLocations" value="classpath:cn/wzk/domain/*.hbm.xml"/>
    </bean>

    <!-- 配置事务管理(做什么) -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="crudAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRES_NEW"/>
            <tx:method name="add*" propagation="REQUIRES_NEW"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="list*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut expression="execution(* cn.wzk.service.*Service.*(..))" id="txPoint"/>
        <aop:advisor advice-ref="crudAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!-- dao -->
    <bean id="baseDao" abstract="true">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="personDao" class="cn.wzk.dao.impl.PersonDaoImpl" parent="baseDao"/>
    <!-- service -->
    <bean id="personService" class="cn.wzk.service.impl.PersonServiceImpl">
        <property name="personDao" ref="personDao" />
    </bean>
    <!-- action -->
    <bean id="personAction" class="cn.wzk.action.PersonAction">
        <property name="service" ref="personService"/>
    </bean>

在配置SessionFactory的时候我们可以保留hibernate.cfg.xml,然后通过configLocation进行加载。

二、Spring+Struts2

1、拷贝jar包

这里写图片描述

最后我们不要忘记了添加一个Struts和Spring的整合包
struts2-spring-plugin-2.3.16.1.jar

2、配置web.xml(Struts2核心过滤器)
<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>/*</url-pattern>
    </filter-mapping>
2、添加struts.xml文件并配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="ssh" extends="struts-default" namespace="/">
        <action name="person_*" class="personAction" method="{1}">
            <result name="list">list.jsp</result>
            <result name="del" type="redirectAction">person_getAll</result>
        </action>
    </package>
</struts>

注意class中填写的是由Spring管理的bean的id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值