本文主要讲ssh(Spring+Struts+Hibernate)的整合思路。
第一步:我们需要添加jar包,在maven项目中我们可以通过pom.xml在自动添加jar包。而传统非maven项目我们需要把jar包拷贝到WebContent>WEB-INF-lib中。
第二步:我们进行Spring配置。首先我们在web.xml中配置Spring核心监听器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
然后在web.xml中配置Spring配置文件的文件名:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
注:如果不添加这个配置默认Spring配置文件为WEB-INF目录下applicationContent.xml的文件。
applicationContext.xml中主要包括以下几个配置:dataSource,sessionFactory,事物管理,JavaBean。
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
" default-autowire="byName">
<!--自动扫描与装配-->
<context:component-scan base-package="com.hhiy" />
<!--数据源配置--> <!-- 配置数据源 -->
<bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- sessionFaction -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:org/entity</value>
</list>
</property>
</bean>
<!-- 事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="mycut" expression="execution(* org.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>
</aop:config>
</beans>
第二步:我们进行hibernate配置。在第一步中我们把hibernate和spring进行了整合,所以此处是对单独配置hibernate进行补充。
我们在applicationContext.xml中的sessionFactory中增加以下这个属性,来添加hibernate配置文件。
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
然后配置hibernate.cfg,xml:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/model/User.hbm.xml"/>
</hibernate-configuration>
第三步:我们进行Struts配置。首先我们在web.xml中配置Spring核心监听器:
我们在web.xml中添加struts核心过滤器:
<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>
然后配置添加struts配置文件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>
<!-- 配置为开发模式 -->
<constant name="struts.devMode" value="true"></constant>
<!-- 扩展名配置为action -->
<constant name="struts.action.extension" value="action"></constant>
<!-- 把主题配置为simple -->
<constant name="struts.ui.theme" value="simple" />
<!-- 网站端package -->
<package name="default" namespace="/member" extends="struts-default">
<!-- 配置测试用的Action -->
<!-- 通配符*,method="{1}"动态确定对应的方法,test_list user_addUI -->
<action name="test_*" class="testAction" method="{1}">
<result name="list">/WEB-INF/jsp/testAction/list.jsp</result>
</action>
<action name="*_*" class="com.hhit.action.{1}" method="{2}">
<result name="success">${dynamicUrl}</result>
<result name="redirect">${dynamicUrl}</result>
<!-- <result type="redirectAction">
<param name="actionName">${actionName}</param>
<param name="namespace" >"/"</param>
</result> -->
<result name="toSecond" type="chain">${dynamicUrl}</result>
<result name="freemark" type="freemarker">${dynamicUrl}</result>
</action>
</package>
<package name="manager" namespace="/manager" extends="struts-default">
<interceptors>
<!-- <interceptor name="managerAuth" class="com.hhit.interceptor.CheckPrivilegeInterceptor" /> -->
<interceptor-stack name="all">
<!-- <interceptor-ref name="managerAuth"/> -->
<interceptor-ref name="defaultStack"/><!--默认拦截器不要丢了,不然无法接受前台传过来的参数 -->
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="all"></default-interceptor-ref>
<!-- 全局的Result配置 -->
<action name="*_*" class="com.hhit.action.{1}" method="{2}">
<result name="success">${dynamicUrl}</result>
<result name="redirect">${dynamicUrl}</result>
<!-- <result type="redirectAction">
<param name="actionName">${actionName}</param>
<param name="namespace" >"/"</param>
</result> -->
<result name="freemark" type="freemarker">${dynamicUrl}</result>
</action>
</package>
</struts>