Spring+Struts2+Hibernate整合配置

Spring+Struts2+Hibernate整合

-配置web.xml

在web.xml中添加过滤器和监听器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--添加监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <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>

</web-app>

-配置spring的配置文件applicationContext.xml

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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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">

    <!--引入外部spring配置文件/service的包扫描-->
    <import resource="classpath:main/resources/spring/spring-*.xml"/>

    <!-- 导入外部的properties配置文件 -->
    <context:property-placeholder location="classpath:main/resources/db.properties" />

    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="jdbcUrl" value="${jdbcUrl}"></property>
    <property name="driverClass" value="${driverClass}"></property>
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>

        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
    <property name="initialPoolSize" value="${initialPoolSize}"></property>

        <!--连接池中保留的最小连接数。Default: 3 -->
    <property name="minPoolSize" value="3"></property>

        <!--连接池中保留的最大连接数。Default: 15 -->
    <property name="maxPoolSize" value="${maxPoolSize}"></property>

        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
    <property name="acquireIncrement" value="3"></property>

        <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800"></property>
    </bean>

    <!--配置hibernate相关属性-->
    <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.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>

        <property name="mappingLocations">
            <list>
                <!--<value>classpath:cn/itcast/nsfw/*/entity/*.hbm.xml</value>-->
                <value>classpath:main/java/entity/*.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!--配置映射-->
    <bean id="personDao" class="main.java.daoImpl.PersonDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>

-配置service、dao等层的包扫描文件

下面是我配置的service层的包扫描文件(需要引入到spring的配置文件中)

spring-service.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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">

    <!--扫描service-->
    <context:component-scan base-package="text.java.service.serviceImpl"/>

</beans>

-配置 struts2

struts.xml(需放到str目录下)

<?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>

    <!-- 禁用动态方法访问 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 配置成开发模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- 配置拓展名为action -->
    <constant name="struts.action.extention" value="action" />
    <!-- 把主题配置成simple -->
    <constant name="struts.ui.theme" value="simple" />

<!--包含配置文件-->
    <include file="main/resources/struts/test-struts.xml"></include>
</struts>

test-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="test-action" namespace="/" extends="struts-default">
        <action name="test_*" class="text.java.action.TestAction" method="{1}">
            <result name="success">/WEB-INF/jsp/test/test.jsp</result>
        </action>
    </package>


</struts>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值