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>