SSH框架环境搭建

来源:互联网 发布:汉唐香莎温泉会所js 编辑:程序博客网 时间:2024/05/21 10:25
【框架概述】
    SSH为Struts+Spring+Hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架。其中Struts作为系统的整体基础架构,负责MVC的分离;利用Hibernate框架对持久层提供支持;Spring做管理,管理Struts和Hibernate.
【具体环境】
    JDK1.8+tomcat7.0+MyEclipse10+MySQL5.6
【整合说明】
    整个过程都是先把每个框架对应的jar包添加好,再新建对应的配置文件。因为Spring是用来管理其他两个框架,所以我们先把另两个框架搭好,最后用Spring分别对Struts和Hibernate进行整合。
【整合步骤】
    1. 新建web工程,添加JUnit
        通过不同的IDE新建一个web工程,这里需要注意的是养成一个很好的习惯,新建完项目后首先去设置器编码格式为UTF-8,从一定程度上避免乱码问题。 
        右击项目—>buid path—>Add library—>JUnit即可添加JUnit,这样方便我们后面进行测试检验。
    2. 搭建三个框架
       (1)Struts2

            1)所需要的jar包

            2)配置文件(Struts.xmlWeb.xml

[html] view plain copy
  1. Struts.xml:  
  2.         <?xml version="1.0" encoding="UTF-8" ?>  
  3.         <!DOCTYPE struts PUBLIC  
  4.             "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  5.             "http://struts.apache.org/dtds/struts-2.0.dtd">  
  6.           
  7.         <struts>  
  8.             <!-- 配置为开发模式 -->  
  9.             <constant name="struts.devMode" value="true" />  
  10.           
  11.             <!-- 把扩展名配置为action -->  
  12.             <constant name="struts.action.extension" value="action"/>  
  13.             <!-- 把主题配置为simple -->  
  14.             <constant name="struts.ui.theme" value="simple"/>  
  15.               
  16.             <package name="default" namespace="/" extends="struts-default">  
  17.               
  18.                 <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->  
  19.                 <action name="test" class="testAction">  
  20.                     <result name="success">/test.jsp</result>  
  21.                 </action>  
  22.                   
  23.             </package>  
  24.           
  25.             <!-- Add packages here -->  
  26.           
  27.         </struts>  
  28. Web.xml:  
  29.     <?xml version="1.0" encoding="UTF-8"?>  
  30.     <web-app version="2.5"   
  31.         xmlns="http://java.sun.com/xml/ns/javaee"   
  32.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  33.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  34.         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  35.           
  36.     <!-- 配置Struts2的核心过滤器 -->  
  37.       <filter>  
  38.             <filter-name>struts2</filter-name>  
  39.             <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  40.         </filter>  
  41.       
  42.         <filter-mapping>  
  43.             <filter-name>struts2</filter-name>  
  44.             <url-pattern>/*</url-pattern>  
  45.         </filter-mapping>  
  46.       
  47.       <welcome-file-list>  
  48.         <welcome-file>index.jsp</welcome-file>  
  49.       </welcome-file-list>  
  50.     </web-app>  
    完成上述过程后,我们可以新建一个Action类和test.jsp页面进行测试,检查Struts框架是否搭建成功。

[java] view plain copy
  1. @Controller  
  2. @Scope("prototype")  
  3. public class TestAction extends ActionSupport {  
  4.     @Override  
  5.     public String execute() throws Exception{  
  6.         System.out.println("--> TestAction.execute()");  
  7.         System.out.println("调用到action");  
  8.         return "success";  
  9.     }  
  10. }  
    (2)Hibernate:
          1)所需要的jar包

          2)配置文件(hibernate.cfg.xml和*.hbm.xml)

[html] view plain copy
  1. Hibernate.cfg.xml:  
  2.     <!DOCTYPE hibernate-configuration PUBLIC  
  3.             "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.             "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.       
  6.     <hibernate-configuration>  
  7.     <session-factory>  
  8.         <!-- 1.数据库连接信息 -->  
  9.         <property name="hibernate.dialect">  
  10.             org.hibernate.dialect.MySQL5InnoDBDialect  
  11.         </property>  
  12.         <property name="hibernate.connection.driver_class">  
  13.             com.jdbc.mysql.Driver  
  14.         </property>  
  15.         <property name="hibernate.connection.username">root</property>  
  16.         <property name="hibernate.connection.password">111111</property>  
  17.         <property name="hibernate.connection.url">  
  18.             jdbc:mysql://localhost:3306/baiduoa0720  
  19.         </property>  
  20.         <!-- 2.其他配置 -->  
  21.         <property name="show_sql">true</property>  
  22.         <property name="hibernate.hbm2ddl.auto">update</property>  
  23.         <!-- 3.导入映射文件 -->  
  24.         <mapping resource="cn/baidu/oa/domain/User.hbm.xml" />  
  25.     </session-factory>  
  26.     </hibernate-configuration>  
  27.       
  28. User实体类所对应的映射文件,User.hbm.xml:  
  29.     <?xml version="1.0"?>  
  30.     <!DOCTYPE hibernate-mapping PUBLIC   
  31.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  32.         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  33.       
  34.     <hibernate-mapping package="cn.baidu.oa.domain">  
  35.         <class name="User" table="baidu_user">  
  36.             <id name="id">  
  37.                 <generator class="native"></generator>  
  38.             </id>  
  39.             <property name="name"></property>  
  40.         </class>  
  41.     </hibernate-mapping>  

    (3)Spring:
          1)所需要的jar包

          2)配置文件(ApplicationContext.xml)

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.         xmlns:tx="http://www.springframework.org/schema/tx"  
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  7.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  8.               
  9. <span style="white-space:pre">      </span><!-- 自动扫描与转配bean -->  
  10.         <context:component-scan base-package="cn.baidu.oa"></context:component-scan>  
  11.               
  12.         <!-- 导入外部的properties文件 -->  
  13.         <context:property-placeholder location="classpath:jdbc.properties"/>  
  14.                   
  15. </beans>  
    (4)Spring与Struts的整合
         1)添加struts2-spring-plugin jar包
         2)在Struts中的Web.xml中配置Spring容器
[html] view plain copy
  1. <!-- 配置Spring的用于初始化的容器监听器 -->  
  2. <listener>  
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4. </listener>  
  5.                   
  6. <context-param>  
  7.     <param-name>contextConfigLocation</param-name>  
  8.     <param-value>classpath:applicationContext*.xml</param-value>  
  9. </context-param>  
    (5)Spring与Hibernate的整合
         1)在Spring的配置文件中添加SessionFactory的管理
[html] view plain copy
  1. <!-- 配置SessionFactory -->  
  2. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">  
  3.     <!-- 指定Hibernate配置文件的路径 -->  
  4.     <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  5.                       
  6.     <!-- 配置c3p0数据库连接池 -->  
  7.     <property name="dataSource">  
  8.         <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  9.     <!-- 数据库信息 -->  
  10.         <property name="jdbcUrl" value="${jdbcUrl}"></property>  
  11.         <property name="driverClass" value="${driverClass}"></property>  
  12.         <property name="user" value="${user}"></property>  
  13.         <property name="password" value="${password}"></property>  
  14.     <!-- 其它配置 -->  
  15.     <!-- 初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default:3 -->  
  16.         <property name="initialPoolSize" value="3"></property>  
  17.     <!-- 连接池中保留的最大连接数。Default:5 -->  
  18.         <property name="maxPoolSize" value="5"></property>  
  19.     <!-- 连接池中保留的最小连接数。Default:3 -->   
  20.         <property name="minPoolSize" value="3"></property>  
  21.     <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->  
  22.         <property name="acquireIncrement" value="2"></property>   
  23.     <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPreConnection均为0,则缓存关闭。Default:0 -->  
  24.         <property name="maxStatements" value="8"></property>  
  25.     <!-- maxStatementsPreConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default:0 -->  
  26.         <property name="maxStatementsPerConnection" value="5"></property>  
  27.     <!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default:0 -->  
  28.         <property name="maxIdleTime" value="1800"></property>  
  29.         </bean>  
  30.     </property>  
  31. </bean>  
  32.                   
2)在Spring的配置文件中添加声明式事务的管理
[html] view plain copy
  1. <!-- 配置声明式事务管理(采用注解方式 )-->  
  2.     <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">  
  3.         <property name="sessionFactory" ref="sessionFactory"></property>  
  4.     </bean>  
  5.     <tx:annotation-driven transaction-manager="txManager"/>  
这样,可以说SSH框架的整合就完成了。源码下载
【学习心得】

    现在热门流行的框架很多,我们听说过的也许就不少,而我们想每个都掌握,也是不太现实。所以,我们该做的还是把握住每一次学习的机会,点点滴滴去积累,相信以后对各种框架都会更容易上手。


转载自:胡志婷,SSH框架整合.

原创粉丝点击