SSH框架整合

【框架概述】
    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

	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.devMode" value="true" />
			
				<!-- 把扩展名配置为action -->
				<constant name="struts.action.extension" value="action"/>
				<!-- 把主题配置为simple -->
				<constant name="struts.ui.theme" value="simple"/>
				
			    <package name="default" namespace="/" extends="struts-default">
			    
			    	<!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->
			    	<action name="test" class="testAction">
			    		<result name="success">/test.jsp</result>
			    	</action>
			        
			    </package>
			
			    <!-- Add packages here -->
			
			</struts>
	Web.xml:
		<?xml version="1.0" encoding="UTF-8"?>
		<web-app version="2.5" 
			xmlns="http://java.sun.com/xml/ns/javaee" 
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
			xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
			http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
			
		<!-- 配置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>
		
		  <welcome-file-list>
		    <welcome-file>index.jsp</welcome-file>
		  </welcome-file-list>
		</web-app>
    完成上述过程后,我们可以新建一个Action类和test.jsp页面进行测试,检查Struts框架是否搭建成功。

			@Controller
			@Scope("prototype")
			public class TestAction extends ActionSupport {
				@Override
				public String execute() throws Exception{
					System.out.println("--> TestAction.execute()");
					System.out.println("调用到action");
					return "success";
				}
			}
    (2)Hibernate:
          1)所需要的jar包

			

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

	Hibernate.cfg.xml:
		<!DOCTYPE hibernate-configuration PUBLIC
		        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
		
		<hibernate-configuration>
		<session-factory>
			<!-- 1.数据库连接信息 -->
			<property name="hibernate.dialect">
				org.hibernate.dialect.MySQL5InnoDBDialect
			</property>
			<property name="hibernate.connection.driver_class">
				com.jdbc.mysql.Driver
			</property>
			<property name="hibernate.connection.username">root</property>
			<property name="hibernate.connection.password">111111</property>
			<property name="hibernate.connection.url">
				jdbc:mysql://localhost:3306/itcastoa0720
			</property>
			<!-- 2.其他配置 -->
			<property name="show_sql">true</property>
			<property name="hibernate.hbm2ddl.auto">update</property>
			<!-- 3.导入映射文件 -->
			<mapping resource="cn/itcast/oa/domain/User.hbm.xml" />
		</session-factory>
		</hibernate-configuration>
		
	User实体类所对应的映射文件,User.hbm.xml:
		<?xml version="1.0"?>
		<!DOCTYPE hibernate-mapping PUBLIC 
			"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
			"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
		
		<hibernate-mapping package="cn.itcast.oa.domain">
			<class name="User" table="itcast_user">
				<id name="id">
					<generator class="native"></generator>
				</id>
				<property name="name"></property>
			</class>
		</hibernate-mapping>

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

          2)配置文件(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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
			
		<!-- 自动扫描与转配bean -->
		<context:component-scan base-package="cn.itcast.oa"></context:component-scan>
			
		<!-- 导入外部的properties文件 -->
		<context:property-placeholder location="classpath:jdbc.properties"/>
				
</beans>
    (4)Spring与Struts的整合
         1)添加struts2-spring-plugin jar包
         2)在Struts中的Web.xml中配置Spring容器

<!-- 配置Spring的用于初始化的容器监听器 -->
<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>
    (5)Spring与Hibernate的整合
         1)在Spring的配置文件中添加SessionFactory的管理

<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<!-- 指定Hibernate配置文件的路径 -->
	<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
					
	<!-- 配置c3p0数据库连接池 -->
	<property name="dataSource">
		<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<!-- 数据库信息 -->
		<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="3"></property>
	<!-- 连接池中保留的最大连接数。Default:5 -->
		<property name="maxPoolSize" value="5"></property>
	<!-- 连接池中保留的最小连接数。Default:3 --> 
		<property name="minPoolSize" value="3"></property>
	<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default:3 -->
		<property name="acquireIncrement" value="2"></property> 
	<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPreConnection均为0,则缓存关闭。Default:0 -->
		<property name="maxStatements" value="8"></property>
	<!-- maxStatementsPreConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default:0 -->
		<property name="maxStatementsPerConnection" value="5"></property>
	<!-- 最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default:0 -->
		<property name="maxIdleTime" value="1800"></property>
		</bean>
	</property>
</bean>
				
2)在Spring的配置文件中添加声明式事务的管理

<!-- 配置声明式事务管理(采用注解方式 )-->
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="txManager"/>
这样,可以说SSH框架的整合就完成了。源码下载

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



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值