Java的SSH框架整合

写了好多篇的Android代码了,在写几篇关于Java的,博客园里肯定都是java的前辈啊,写的不好多给意见。

SSH,什么是SSH呢,Struts+Spring+Hibernate,这三个就是整个的SSH了。

集成SSH框架的系统从职责上分为四层:表示层、业务逻辑层、数据持久层和域模块层,以帮助开发人员在短期内搭建结构清晰、可复用性好、维护方便的Web应用程序。其中使用Struts作为系统的整体基础架构,负责MVC的分离,在Struts框架的模型部分,控制业务跳转,利用Hibernate框架对持久层提供支持,Spring做管理,管理struts和hibernate。具体做法是:用面向对象的分析方法根据需求提出一些模型,将这些模型实现为基本的Java对象,然后编写基本的DAO(Data Access Objects)接口,并给出Hibernate的DAO实现,采用Hibernate架构实现的DAO类来实现Java类与数据库之间的转换和访问,最后由Spring做管理,管理struts和hibernate。

,
Jar包的链接:链接:http://pan.baidu.com/s/1gfv7EYB 密码:qrpf
如果链接失效了,发我的邮箱,我给你们发送dingchao7323@qq.com

需要的Jar包:

首先看配置applicationContext.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: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.1.xsd
    
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Spring和Hibernate的整合分为两种 
        第一种:加载整个hibernate配置文件
        第二种:首先配置数据与然后配置会话工厂
    -->
    <!-- 这个是需要hibernate.cfg。xml文件 -->
    <!-- 第一种配置:加载整个hibernate配置文件   1
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
     -->
     <!-- ==============================================第二种方式===================================================== -->
     <!-- 第二种不要sessionFactory    2            2 -->
     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
         <property name="url" value="jdbc:oracle:thin:@localhost:1521:****"></property>
         <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
         <property name="username" value="****"></property>
         <property name="password" value="****"></property>
     </bean>
     <!-- 加载数据库连接            2-->
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="dataSource" ref="dataSource"></property>
         <!-- 加载实体类配置文件 -->
         <!-- 加载工具例如 show_sql -->
         <property name="hibernateProperties">
             <props>
                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                 <prop key="hibernate.show_sql">true</prop>
                 <prop key="hibernate.format_sql">true</prop>
             </props>
         </property>
         <!-- 加载实体类配置文件 -->
         <!-- 第二种的   另外另种配置实体类方式    1   这一个是找的具体的***。hbm.xml文件-->
         <!-- <property name="mappingResources">
             <list>
                 <value>com/dc/entity/Dept.hbm.xml</value>
                 <value>com/dc/entity/Emp.hbm.xml</value>
             </list>
         </property> -->
         <!-- 第二种的   另外另种配置实体类方式    2  这个是找的所在的包 -->
      <property name="mappingDirectoryLocations">
             <list>
                 <value>classpath:com/dc/entity/</value>
             </list>
         </property> 
     </bean>
      <!-- ==============================================第二种方式===================================================== -->
     
     <!-- 只是上边不一样,这俩个都有 -->
    <!-- hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- ============================Dao的配置 ============================-->
    <bean id="empDao" class="com.dc.dao.impl.SaleOrderLineDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
                <!-- SaleOrderDaoImpl -->
    <bean id="SaleOrderDao" class="com.dc.dao.impl.SaleOrderDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    
    <!-- ============================Service的配置============================ -->
    <bean id="empService" class="com.dc.service.impl.SaleOrderLineServiceImpl">
        <property name="soldao" ref="empDao"></property>
    </bean>
            <!-- SaleOrderServiceImpl -->
    <bean id="SaleOrderService" class="com.dc.service.impl.SaleOrderServiceImpl">
        <property name="sodao" ref="SaleOrderDao"></property>
    </bean>
    
    <!-- ============================Action的配置============================ -->
    <bean id="empAction" class="com.dc.action.SaleOrderLineAction">
        <property name="solSer" ref="empService"></property>
    </bean>
    
     <bean id="getInfoById" class="com.dc.action.SaleOrderLineAction">
        <property name="solSer" ref="empService"/>
    </bean>
        
        <bean id="getMaxOdrId" class="com.dc.action.SaleOrderAction">
            <property name="soSer" ref="SaleOrderService"></property>
        </bean>
        <bean id="addSaleOrder" class="com.dc.action.SaleOrderAction">
            <property name="soSer" ref="SaleOrderService"></property>
        </bean>
    
    <!-- ==============================================配置事物管理器===================================================== -->
    <!-- 配置事务管理器 -->
    <bean id="txTran" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 声明式事物 -->
    <!-- <tx:method name="" read-only="false" propagation="REQUIRED"></tx:method> read-only查询时使用  propagation修改时使用 -->
    <tx:advice id="txAdvice" transaction-manager="txTran">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="up*" propagation="REQUIRED"/>
            <tx:method name="dl*" propagation="REQUIRED"/>
            <!-- 下面的是查询的 -->
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="ser*" read-only="true"/>
            <tx:method name="load*" read-only="true"/>
            
        </tx:attributes>
    </tx:advice>
    
    <!--  -->
    <aop:config>
        <aop:pointcut expression="execution(* com.dc.dao.*.*(..))" id="pointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
    
    
</beans>

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>

    <!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 该属性设置Struts 2应用是否使用开发模式。如果设置该属性为true,则可以在应用出错时显示更多、更友好的出错提示。该属性只接受true和flase两个值,该属性的默认值是false。通常,应用在开发阶段,将该属性设置为true,当进入产品发布阶段后,则该属性设置为false。 -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.configuration.xml.reload" value="true" />
    <constant name="strus.objectFactory" value="spring"></constant>
    <package name="default" namespace="/" extends="struts-default">

        <action name="show" class="empAction">
            <result name="success">findAllSaleOrderLine.jsp</result>
        </action>
        <!-- <action name="getSaleOrderLineInfoByoolId" class="com.dc.action.SaleOrderLineAction" 
            method="getInfoById"> <result name="success">getSaleOrderLineInfoByoolId.jsp</result> 
            </action> -->

        <!-- 不建议使用这个,配合上边的constant -->
        <action name="getSaleOrderLineInfoByoolId" class="getInfoById"
            method="getInfoById">
            <result name="success">getSaleOrderLineInfoByoolId.jsp</result>
        </action>

        <action name="getMaxOdrId" class="getMaxOdrId" method="getMaxOdrId">
            <result name="success">addSaleOrder.jsp</result>
        </action>

        <action name="addSaleOrder" class="addSaleOrder" method="addSaleOrder">
            <result name="success" type="redirectAction">show</result>
            <result name="error" type="redirectAction">getMaxOdrId</result>
            <!-- <result name="input">index.jsp</result> -->
        </action>
    </package>

</struts>

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_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Spring_04_Sale</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <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>
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <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>
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>com.dc.util.EncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
原文地址:https://www.cnblogs.com/dingxiansen/p/6057760.html