4、SSH集成笔记

1.各框架版本如下:

  • spring-framework-3.2.4.RELEASE-dist.zip
  • struts-2.3.8-all.zip
  • hibernate-release-4.2.8.Final.zip
2.目录结构如下:
3.用到的jar包如下:
4.web.xml文件内容如下:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>ssh2</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <!-- 配置struts过滤器 -->
    <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>
    <!-- 加载spring配置文件applicationContext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 指明spring配置文件在何处 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml,classpath*:applicationContext*.xml</param-value>
    </context-param>
</web-app>
 
5.applicationContext.xml文件内容如下(事务还有注入bean都是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: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/context 
           http://www.springframework.org/schema/context/spring-context-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"
>
    <!-- 数据源配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="org.gjt.mm.mysql.Driver" />
        <!-- url配置中将&号写为&由于&是xml保留字 -->
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=utf8" />
        <property name="user" value="root" />
        <property name="password" value="123456" />
        <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="1" />
        <!--连接池中保留的最小连接数。 -->
        <property name="minPoolSize" value="1" />
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="300" />
        <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="60" />
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="5" />
        <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
        <property name="idleConnectionTestPeriod" value="60" />
    </bean>
    <!-- sessionFactory配置 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>com/zmp/domain/User.hbm.xml</value>
            </list>
        </property>
        <!-- hibernate属性配置 -->
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=false
                hibernate.format_sql=false
                hibernate.connection.autocommit=true
            </value>
        </property>
    </bean>
    <!-- 事务管理配置,使用spring很大一部分原因是用spring的事务管理器 -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <!-- 由于要通过session开启事务,需要提供sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 事务的传播性 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="select*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- aop面向切面编程,采用面向切面的方法,对符合表达式的切入点加入事务 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut expression="execution(* com.zmp.service..*.*(..))" id="transactionPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
    </aop:config>
    
    <!-- UserDao组件注入 -->
    <bean id="userDao" class="com.zmp.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- UserService组件注入 -->
    <bean id="userService" class="com.zmp.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao" />
    </bean>
    <!-- 为LoginAction注入依赖对象 -->
    <bean id="loginAction" class="com.zmp.action.LoginAction">
        <property name="userService" ref="userService" />
    </bean>
</beans>
 
6.struts.xml文件内容如下:
 
<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="utf-8"></constant>
    <constant name="struts.objectFactory" value="spring"></constant>
    <!-- 此配置即说明由spring创建Action实例,将struts交给spring管理 -->
    <constant name="struts.objectFactory" value="spring" />
    <package name="login" namespace="/login" extends="struts-default">
        <!-- 此处用loginAction而不用com.zmp.action.LoginAction是因为spring已为其创建了实例 -->
        <action name="check" class="loginAction" method="execute">
            <result name="success">/success.html</result>
            <result name="failed">/failed.html</result>
        </action>
    </package>
</struts>
 
 
 
 
 
 
 
 
posted @ 2014-04-06 17:47  zmpandzmp  阅读(209)  评论(0编辑  收藏  举报