ssh快速整合

来源:互联网 发布:源码分享是什么意思 编辑:程序博客网 时间:2024/04/30 15:02

虽然ssh整合不难,但是步骤繁琐,极易出错。为了减少开发时间,小编给出一套简单快速的方法:

前提:struts2.1.8+spring2.5+hibernate3.3

数据库:oracle

开发工具:myeclipse

使用的jar包:

上面的包也许不会全部用到,但是为了项目的综合考虑,还是导入为好,其中struts2-spring-plugin-2.1.8.jar一定要引入!!!

下面开始整合:

1.整合spring

在src目录下建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">

然后写一个Test类测试,并在上面的代码下面添加:

<bean id="testService" class="com.hsp.test.testService">
  <property name="name" value="ok"/>
  </bean>

2.整合hibernate

在这里,小编强烈建议使用hibernate的逆向工程,即自己先在myeclipse中连接到数据库,然后逆向生成每个表的java bean/domain/pjob对象,可以先暂时把lib目录下的jar包删除,引入myeclipse的jar包,待逆向后,再删除刚刚引进的jar包,而导入上面图片的包。逆向生成后,hibernate也就整合好了。

3.整合struts

在src目录下建strut.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">

在web.xml中输入:

<!-- 配置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>

4.spring接管hibernate

在ApplicationContext.xml中输入:

<!-- 启动注解扫描 -->
  <context:annotation-config/>

<!-- 配置数据源 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/flyme?useUnicode=true&amp;characterEncoding=utf-8"/>
   <property name="username" value="root"/>
   <property name="password" value="root"/>
  <!-- 连接池启动时的初始值 -->
  <property name="initialSize" value="3"/>
  <!-- 连接池的最大值 -->
  <property name="maxActive" value="500"/>
  <!-- 最大空闲值,但经过一个高峰时间后,连接池会将不用的连接释放一部分 -->
  <property name="maxIdle" value="2"/>
  <!-- 最小空闲值,空闲时连接池会申请连接,以免高峰来临时造成连接时间长 -->
  <property name="minIdle" value="1"/>
  </bean>

<!-- 配置会话工厂 -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <!-- 设置数据源 -->
  <property name="dataSource" ref="dataSource"/>
  <!-- 接管hibernate对象映射文件 -->
  <property name="mappingResources">
  <list>

<!--这里看你的项目的表而定-->
  <value>com/hsp/domain/Employee.hbm.xml</value>
  <value>com/hsp/domain/Department.hbm.xml</value>
  <value>com/hsp/domain/Biji.hbm.xml</value>
  </list>
  </property>
  <property name="hibernateProperties">
  <value>
  hibernate.dialect=org.hibernate.dialect.OracleDialect
  hibernate.hbm2ddl.auto=update
  hibernate.show_sql=true
  </value>
  </property>
  </bean>

 <!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
  </beans> 

5.spring接管struts2

在struts.xml中输入:

 <struts>
    <constant name="struts.objectFactory" value="spring"/>

    </struts>

在web.xml中输入:

 <!-- 指定spring的配置文件,默认从web根目录下寻找文件,通过从spring提供的classpath:前缀指定从类路径开始寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<!-- 即可以简单理解为tomcat初始化spring的applicationContext.xml-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这里注意要将以上代码放在中间

6.添加spring提供的一些过滤器

同样在web.xml的中间添加:

<!-- spring提供的过滤器 -->
  <filter>
  <filter-name>encoding</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 该过滤器作用类似于取消懒加载,会在需要关联时向数据库发送请求 -->
<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>/*</url-pattern>  
   </filter-mapping>

7.写一个简单的登录功能测试

8.整合完毕

0 0