SSH框架整合开发详解(个人笔记)

来源:互联网 发布:淘宝首页推广费用 编辑:程序博客网 时间:2024/04/29 09:18

一.创建数据库并设置编码。

A) create database oa default character set utf8

二.MyEclipse工程

A) 在Myeclipse里创建web工程,并设置编码为utf8.

B) 添加框架环境

1.添加Junit4 libraryMyeclipse自带)

2.添加Struts2环境

①所需Jar

②配置文件:拷贝一个struts.xml模版到src目录,进行适当修改,web.xml里配上需要的代码。

struts.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.   
  8.     <!-- 配置为开发模式 -->  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <!-- 把扩展名配置为action -->  
  11.     <constant name="struts.action.extension" value="action" />  
  12.     <!-- 把主题配置为simple -->  
  13.     <constant name="struts.ui.theme" value="simple" />  
  14.       
  15.      
  16.     <package name="default" namespace="/" extends="struts-default">  
  17.         <action name="" class="">  
  18.             <result name=""></result>  
  19.         </action>  
  20.     </package>  
  21.     <!-- Add packages here -->  
  22.       
  23. </struts>  

web.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 配置Struts2的核心的过滤器 -->  
  2. <filter>  
  3.     <filter-name>struts2</filter-name>  
  4.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  5. </filter>  
  6. <filter-mapping>  
  7.     <filter-name>struts2</filter-name>  
  8.     <url-pattern>/*</url-pattern>  
  9. </filter-mapping>  



3.添加hibernate环境 

①所需Jar

②配置文件:拷贝一个hibernate.cfg.xml和映射文件*.hbm.xml模版到src目录,并进行适当修改。

hibernate.cfg.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version='1.0' encoding='utf-8'?>  
  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.   
  8. <session-factory>  
  9.   
  10.     <!-- 1,数据库连接信息 -->  
  11.     <property name="dialect">  
  12.         org.hibernate.dialect.MySQL5InnoDBDialect  
  13.     </property>  
  14.     <property name="connection.url">jdbc:mysql:///oa</property>  
  15.     <property name="connection.driver_class">com.jdbc.mysql.Driver</property>  
  16.     <property name="connection.username">root</property>  
  17.     <property name="connection.password">root</property>  
  18.   
  19.     <!-- 2,其他配置 -->  
  20.     <property name="show_sql">true</property>  
  21.     <property name="hbm2ddl.auto">update</property>  
  22.   
  23.     <!-- 3,导入映射文件 -->  
  24.     <mapping resource="" />  
  25.       
  26. </session-factory>  
  27.   
  28. </hibernate-configuration>  

User.hbm.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping package="cn.grace.oa.domain">  
  7.   
  8.     <class name="User" table="grace_user">  
  9.         <id name="id">  
  10.             <generator class="native"/>  
  11.         </id>  
  12.         <property name="name" />  
  13.     </class>  
  14.       
  15. </hibernate-mapping>  

4.添加spring环境

①所需Jar

②配置文件:拷贝一个appicationContext.xml/beans.xml模版到src目录

appicationContext.xml(较完整版)

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.     <!-- 自动扫描与装配bean,包括子包 -->  
  10.     <context:component-scan base-package="cn.itcast.oa"></context:component-scan>  
  11.   
  12.       
  13. <!-- 导入外部的properties文件 -->  
  14. <context:property-placeholder location="classpath:jdbc.properties"/>  
  15.   
  16.   
  17.     <!-- 配置SessionFactory IOC-->  
  18.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  19.         <!-- 指定hibernate的配置文件位置 -->  
  20.         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  
  21.         <!-- 配置c3p0数据库连接池 -->  
  22.           
  23.         <property name="dataSource">  
  24.             <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  25.                 <!-- 数据连接信息 -->  
  26.                 <property name="jdbcUrl" value="${jdbcUrl}"></property>  
  27.                 <property name="driverClass" value="${driverClass}"></property>  
  28.                 <property name="user" value="${user}"></property>  
  29.                 <property name="password" value="${password}"></property>  
  30.                   
  31.                 <!-- 其他配置 -->  
  32.                 <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->  
  33.                 <property name="initialPoolSize" value="3"></property>  
  34.                 <!--连接池中保留的最小连接数。Default: 3 -->  
  35.                 <property name="minPoolSize" value="3"></property>  
  36.                 <!--连接池中保留的最大连接数。Default: 15 -->  
  37.                 <property name="maxPoolSize" value="5"></property>  
  38.                 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->  
  39.                 <property name="acquireIncrement" value="3"></property>  
  40.                 <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->  
  41.                 <property name="maxStatements" value="8"></property>  
  42.                 <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->  
  43.                 <property name="maxStatementsPerConnection" value="5"></property>  
  44.                 <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->  
  45.                 <property name="maxIdleTime" value="1800"></property>  
  46.             </bean>  
  47.         </property>  
  48.     </bean>  
  49.   
  50.   
  51.     <!-- 配置声明式事务管理(采用注解的方式) AOP-->  
  52.     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  53.         <property name="sessionFactory" ref="sessionFactory"></property>  
  54.     </bean>  
  55.     <!-- 注解驱动-->  
  56.     <tx:annotation-driven transaction-manager="txManager"/>  
  57.    
  58.   
  59. </beans>  

5.整合SpringStruts2 (IOC:让struts2action交由容器管理)


src目录下建Junit测试包进行测试

①单独测试struts2,新建TestAction

a) TestAction.Java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class TestAction extends ActionSupport{  
  2.       
  3.     @Override  
  4.     public String execute() throws Exception{  
  5.         System.out.println("===>TestAction.execute()");  
  6.         return "success";  
  7.     }  
  8. }  

b)在struts.xml里配置相应action

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->  
  2. <action name="test" class="cn.grace.oa.test.TestAction">  
  3.     <result name="success">/test.jsp</result>  
  4. </action>  

c)部署测试是否可以通过test.action正常访问test.jsp页面和是否输出"===>TestAction.execute()"


②单独测试Spring,新建SpringTest此时appicationContext.xml里面只需要一个装配和扫描bean的语句,否则可能报错

a) SpringTest.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class SpringTest {  
  2.     private ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");  
  3.       
  4.     @Test  
  5.     public void testBean() throws Exception{  
  6.         TestAction testAction=(TestAction) ac.getBean("testAction");  
  7.         System.out.println(testAction);  
  8.     }  
  9. }  

b)此时Junit测试不能成功,必须在TestAction类上进行声明,并配置scope

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Controller  
  2. @Scope("prototype")  
  3. public class TestAction extends ActionSupport{  
  4.       
  5.     @Override  
  6.     public String execute() throws Exception{  
  7.         System.out.println("==>TestAction.execute");  
  8.         return "success";  
  9.     }  
  10. }  
c)此时再进行Junit测试成功输出testAction.


d)声明一个bean(注解方式,有四种方式,根据不同类用相对应的方式)

效果跟在bean.xml里面写<bean id="" class=""></bean>功能是一样的

@Component("beanName")//不写beanName默认使用类名首字母小写,即testAction

@Controller

@Service

@Repository

配置beanscope,默认为单例,修改为prototype

@Scope("prototype")


③测试Struts2Spring整合

a)在web.xml中配置一个监听器

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 配置Spring的用于初始化容器对象的监听器 -->  
  2. <listener>  
  3.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  4. </listener>  
  5. <context-param>  
  6.     <param-name>contextConfigLocation</param-name>  
  7.     <param-value>classpath:applicationContext*.xml</param-value>  
  8. </context-param>  

b) 加一个jar包:struts2-spring-plugin-2.1.8.1.jar

struts.xml中的actionclass从路径改为bean名称,再进行测试,看是否整合成功

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 -->  
  2. <!-- 当Struts2与Spring整合后,class属性可以写bean的名称 -->  
  3. <action name="test" class="testAction">  
  4.     <result name="success">/test.jsp</result>  
  5. </action>  

c)部署测试是否可以通过test.action正常访问test.jsp页面正常访问test.jsp代表整合成功。


6.整合SpringHibernate1.管理SessionFactory实例(只需要一个)2.声明式事务管理)

SessionFactory

a)applicationContext.xml里配置sessionFactoryv(见上文appicationContext.xml),并在hibernate.cfg.xml去掉重复的连接数据库的信息

b)新建一个jdbc.properties用来存放dataSource用到的数据库连接信息,并在applicationContext.xml导入该文件

applicationContext.xml(上文appicationContext.xml已配置)

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 导入外部的properties文件 -->  
  2. <context:property-placeholder location="classpath:jdbc.properties"/>  

jdbc.properties

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. jdbcUrl     = jdbc:mysql:///oa?characterEncoding=utf8  
  2. driverClass = com.mysql.jdbc.Driver  
  3. user        = root  
  4. password    = root  

c)SpringTest类里写测试方法,进行Junit测试。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2. public void testSessionFactory() throws Exception{  
  3.     SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory");  
  4.     System.out.println(sessionFactory);  
  5. }  

声明式事务管理

a)applicationContext.xml里配置声明式事务管理(上文appicationContext.xml已配置)

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 配置声明式事务管理(采用注解的方式) AOP-->  
  2. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  3.     <property name="sessionFactory" ref="sessionFactory"></property>  
  4. </bean>  
  5. <!-- 注解驱动-->  
  6. <tx:annotation-driven transaction-manager="txManager"/>  

b)测试声明式事务管理(测试回滚)

i.新建一个用于测试的实体类:User.java(只需定义idname及其构造方法即可)

ii.根据User写对应的User.hbm.xml映射文件

iii.hibernate.cfg.xml导入映射文件

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <mapping resource="cn/grace/oa/domain/User.hbm.xml" />  

iV.新建一个TestService类

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Service("testService")  
  2. public class TestService {  
  3.       
  4.     @Resource  
  5.     private SessionFactory sessionFactory;  
  6.       
  7.     @Transactional  
  8.     public void saveTwoUsers(){  
  9.         Session session=sessionFactory.getCurrentSession();  
  10.         session.save(new User());  
  11.         int a=1/0;//会抛异常,因为声明了事务,因此会回滚  
  12.         session.save(new User());  
  13.     }  
  14. }  

V.在SpringTest类里写测试方法,进行Junit测试。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Test  
  2. public void testTransaction() throws Exception{  
  3.     TestService testService=(TestService)ac.getBean("testService");  
  4.     testService.saveTwoUsers();  
  5. }  

Vi如果数据库中有新创建的表,并且没有插入数据,则去掉异常int a=1/0;再进行正常插入,如果此时id 从2开始,则 测试成功,因为id为1的user在之前一次测试中,抛异常被回滚。至此,三大框架整合完毕。


7.对三大框架整合进行完整测试。

①修改TestAction.java为如下代码

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Controller  
  2. @Scope("prototype")  
  3. public class TestAction extends ActionSupport{  
  4.       
  5.     @Resource  
  6.     private TestService testService;  
  7.     @Override  
  8.     public String execute() throws Exception{  
  9.         //System.out.println("test");  
  10.         testService.saveTwoUsers();  
  11.         return "success";  
  12.     }  
  13. }  

②Spring管理对象(事务),action处理请求,hibernate处理对象的存储。

③如果此时通过test.action能正常显示test.jsp并且在数据库中的user表中增加了2条记录,则整合成功。(可以再加入异常进行反复测,如果还出现回滚现象,则代表成功)

0 0