SSH整合步骤

这几天打算深入学习ssh,这里总结了ssh整合的基本步骤,
ssh整合步骤:
1.导入必要的jar包;
2.在web.xml中配置对spring的支持;

在web.xml中加入如下代码.

<!-- Spring 载入上下文监听器 -->
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
      classpath*:application-context.xml
    </param-value>
   </context-param>

   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

3.创建spring配置文件.
文件名与web.xml中配置的名字要一致,在上面的配置文件中名为application-context.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"
   xmlns:context="http://www.springframework.org/schema/context"
   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
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
</beans>

4.spring与hibernate集成.配置datasource以及sessionFactory.
a.配置datasource数据源.这里以mysql为例.其中包含了连接池的设置,这里暂时不做介绍.代码如下:

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/ssh?createDatabaseIfNotExist=true" />
      <property name="username" value="root" />
      <property name="password" value="sql" />
      <property name="defaultAutoCommit" value="true" />
      <property name="maxActive" value="100" />
      <property name="initialSize" value="5" />
      <property name="maxWait" value="1000" />
      <property name="maxIdle" value="20" />
      <property name="minIdle" value="3" />
      <property name="removeAbandoned" value="true" />
      <property name="removeAbandonedTimeout" value="180" />
   </bean>

b.配置sessionFactroy

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="hibernateProperties">
         <value>
            hibernate.dialect=org.hibernate.dialect.MySQLDialect
            hibernate.show_sql=true
            hibernate.format_sql=true
         </value>
      </property>
      <!-- 扫描hibernate hbm.xml文件 -->
      <property name="mappingResources">
<list>
<value>*.hbm.xml</value>
</list>
</property>

   </bean>

5.配置hibernate对象关系映射文件,及java类与数据表的映射关系,文件名通常为表名.hbm.xml.该文件放在javabean所在的包.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.cw.x2.auth.common.resac.entity">
    <class name="Org" table="`Org`">
        <id name="code" type="java.lang.String">
            <column name="`code`" length="36" />
            <generator class="uuid.hex" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="`name`" length="100" not-null="true" />
        </property>
        <property name="certType" type="java.lang.String">
            <column name="`certType`" length="4" />
        </property>
        <property name="sponsor" type="java.lang.String">
            <column name="`sponsor`" length="32" />
        </property>
        <property name="country" type="java.lang.String">
            <column name="`country`" length="3" />
        </property>
    </class>
</hibernate-mapping>

6.集成struts2.
a.引入配置文件struts.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>   
   <package name="" extends="struts-default" namespace="/">
      <action name="" class="" method="">
        <result name="success">/index.jsp</result>
      </action>
   </package>
</struts>    

b.在web.xml中加入对struts2的支持,配置struts2请求分发过滤器.默认设置为.action,及所有的后缀为action的请求都会被struts2处理.
 <!-- struts2 action 分发依赖的过滤器 -->
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
   </filter-mapping>

c.将struts2的对象工厂交给spring托管.在struts.xml中加入代码

<!-- struts2委托spring管理 -->
   <constant name="struts.objectFactory" value="spring" />

d.将struts的Action配置给spring.在spring配置文件application-context.xml
中加入如下代码.这里.Action类依赖于Test类,Test类依赖于datasource.scope属性是生命周期的意思,默认值为singleton,生命周期为prototype的bean,每一次请求都会产生一个新的bean实例,相当与一个new的操作,

      <bean id="test" class="supben.Test">
      	 <property name="dataSource" ref="dataSource" />
  	  </bean>
   <bean id="testAction" class="supben.Action" scope="prototype">
  	  		<property name="test" ref="test"/>
  	  </bean>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇