首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

ssh调整步骤

2012-06-26 
ssh整合步骤这几天打算深入学习ssh,这里总结了ssh整合的基本步骤,ssh整合步骤:1.导入必要的jar包2.在web.

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" 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" 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 />        </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="" 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" ref="dataSource" />    </bean>   <bean id="testAction" scope="prototype">    <property name="test" ref="test"/>    </bean>

  实现代码分层,只需要如上面所示,配置每层之间的依赖关系.


到这里,ssh整合以基本配置成功,实际上,还有另外一种配置,及注解配置.可以大大减少配置文件的数量及大小.这里暂时不作介绍.
1 楼 supben 2012-04-16   总结的不错 2 楼 hellostory 2012-04-16   每天一篇SSH整合篇,日复一日,年复一年,什么时候是尽头~
SSH整合真的能像红楼梦一样演变成“红学”吗?

热点排行