SSH框架的组合方式一:Hibernate.cfg.xml由Hibernate自己管理
说明:我开发的环境是:MyEclipse5.5与JDK1.5
1,应先写好包
com.mengya.entity:实本包
com.mengya.dao:DAO接口包
com.mengya.dao.imple实体包
com.megnya.util:uitl包
2,加上Spring支持
3,加上Struts支持
4,加上Hibernate支持
5,由Hibernate创建entity类和DAO
可以在dao中重载一下delete方法,由ID删除对象
由产生的DAO抽出接口来分别把它们分到com.megnya.dao与com.mengya.dao.imple包中
6,在Spring的applicationContext.xml中分为applicationContext-beans.xml(普通的beans)
和applicationContext-comon.xml(公共的beans)(个人习惯问题,可以不用分开)
7,修改applicationContext-comon.xml有DTD
修改为:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
这样写的话后面写起来比较方便
applicationContext-comon.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
<!-- 配置公共的部分 -->
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务传播属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="merge" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config>
<!-- com.mengya.dao.imple下在所有类,所有类的所有方法(..表示任何形式的参数)都是poincut切入点 -->
<aop:pointcut id="allMethod" expression="execution(* com.mengya.dao.imple.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
</beans>
8,加上Struts的action与actionForm
在actionForm中封装Hibernate生成的Javabean
修改struts-config.xml中的action的type将com.mengya.struts.action.StuAction修改为:org.springframework.web.struts.DelegatingActionProxy
9,在Spring中配置Struts的action
Struts中的action的type值改为:org.springframework.web.struts.DelegatingActionProxy(代理)
<bean id="StudentDAO" class="com.mengya.dao.imple.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
注:DAO应继承HibernateDaoSupport
<!-- 配置Struts的action,bean的name值为action的path -->
<bean name="/stu" class="com.mengya.struts.action.StuAction">
<property name="studao" ref="StudentDAO" />
</bean>
10,在web.xml中配置
<!-- 使用Spring提供的encoding的filter -->
<filter>
<filter-name>springfitler</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springfiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 若不在web.xml中配置如下内容则:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
-->
就是在struts-config.xml加上
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="classpath*:applicationContext-*.xml"/>
</plug-in>
11,好了.OK,组合成功!
个人说明:集合后可能会出错
1,可能是少了commons-pool-1.3.jar包(MyEclipse5.5)
2,可能是要重盖hibernate-annotations.jar包(MyEclipse5.1)