首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Struts1.x Spring2.x Hibernate3.x DWR2.x调整笔记

2012-08-10 
Struts1.x Spring2.x Hibernate3.x DWR2.x整合笔记Struts1.xSpring2.x Hibernate3.x DWR2.x整合笔记整合 S

Struts1.x Spring2.x Hibernate3.x DWR2.x整合笔记

Struts1.xSpring2.x Hibernate3.x DWR2.x整合笔记

整合 Struts Spring

1. 在web.xml 中进行 Struts 和Spring 的配置

1.web.xml 中配置 Spring 的Context(必需设置)

<!--源代码目录下面应该有这些文件, 注意,这里用到了通配可匹配多个文件-->

<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>

2.web.xml 中设置的 Struts (自动设置)

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>

org.apache.struts.action.ActionServlet

</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<!-- 可能会有其它的可选启动参数 -->

<load-on-startup>0</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

3.web.xml 中配置字符集过滤器(必需设置)

<filter>

<filter-name>charsetfilter</filter-name>

<filter-class>

org.springframework.web.filter.CharacterEncodingFilter

</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

<init-param>

<param-name>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>charsetfilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

2. 在 Struts 配置文件中进行和 Spring 的集成

4.struts-config.xml 中设置 Spring 插件支持(可选设置)

<plug-in

className="org.springframework.web.struts.ContextLoaderPlugIn">

<set-property property="contextConfigLocation"

value="classpath:applicationContext*.xml" />

</plug-in>

5.struts-config.xml 中设置 action 的 type 属性(必需设置)

type="org.springframework.web.struts.DelegatingActionProxy"

3. 在 Spring 配置文件中进行和 Struts 的集成

6.Sping 配置文件中进行 action 的设置(必需设置)

<!-- name等同于action中的path, class等同于action中type -->

<bean name="/login" class="cn.qdqn.web.action.LoginAction">

<!-- 通常在此会写上biz相关接口的注入 <property...> -->

</bean>

4. 其它推荐配置

7. log4j.properties 文件生成,注意路径必需在 src/目录下(推荐设置)

log4j.rootLogger=WARN, Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender

log4j.appender.Console.layout=org.apache.log4j.PatternLayout

log4j.appender.Console.layout.ConversionPattern=%-5p: %c#%M %x: %m%n

整合 Spring Hibernate

本文采用的是hibernate 配置文件和 spring 配置文件分离式配置

5. Hibernate 配置文件

8.hibernate.cfg.xml (自动生成)

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.-->

<hibernate-configuration>

<session-factory>

<property name="myeclipse.connection.profile">2005</property>

<property name="connection.url">

jdbc:sqlserver://localhost:1433;databaseName=hibernate_db

</property>

<property name="connection.username">sa</property>

<property name="connection.password"></property>

<property name="connection.driver_class">

com.microsoft.sqlserver.jdbc.SQLServerDriver

</property>

<property name="dialect">

org.hibernate.dialect.SQLServerDialect

</property>

<property name="show_sql">true</property>

<mapping resource="cn/qdqn/entity/Country.hbm.xml" />

<mapping resource="cn/qdqn/entity/City.hbm.xml" />

<mapping resource="cn/qdqn/entity/County.hbm.xml" />

</session-factory>

</hibernate-configuration>

6. Spring配置文件

在Spring中需要进行DAO依赖的注入及事务的aop管理,在这里,我们使用2.0的风格

9.spring的数据库相关配置文件头部声明(必需设置)

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<! —具体配置略--

</beans>

10.spring的数据库相关配置文件会话工厂代码(自动生成)

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="configLocation"

value="classpath:hibernate.cfg.xml">

</property>

</bean>

11.spring的数据库相关配置文件DAO依赖注入(自动生成)

<!-- 自动生成DAO类的依赖注入 -->

<bean id="CountryDAO" class="cn.qdqn.entity.CountryDAO">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

在自动生成的DAO中,spring是不管理事务的,所以我们必需自己配置事务管理,通常,我们在biz对象中对DAO进行依赖,对biz对象进行aop的事务管理

12.spring的数据库相关配置文件AOP事务管理2.0风格(必需设置)

<!-- 建立事务管理器 -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref local="sessionFactory" />

</property>

</bean>

<!-- 定义事务通知 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<!-- 对查找方法进行只读事务通知要求查找方法以find开头可按需要修改 -->

<tx:method name="find*" propagation="SUPPORTS"

read-only="true" />

<!-- 对其它方法如增 删 改进行事务支持 -->

<tx:method name="*" propagation="REQUIRED" />

</tx:attributes>

</tx:advice>

<!-- 切面定义 -->

<aop:config>

<!-- 对cn.qdqn.biz包及子包中的任意方法进行切面 -->

<aop:pointcut id="bizMethods"

expression="execution(*cn.qdqn.biz..*.*(..))" />

<aop:advisor advice-ref="txAdvice"pointcut-ref="bizMethods" />

</aop:config>

7.OpenSessionInViewFilter使用方法

在Jsp页面上,如果想使用one-many 等对象关联关系进行数据访问,通常会遇到session已经被关闭的错误,我们需要在web.xml的配置文件中设置OpenSessionInView特性。

<!-- web.xml OpenSessionInView -->

<filter>

<filter-name>OpenSessionInView</filter-name>

<filter-class>

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

</filter-class>

<init-param>

<param-name>sessionFactoryBeanName</param-name>

<!-- 注意下面的值必需和spring配置文件中的工厂id一样 -->

<param-value>sessionFactory</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>OpenSessionInView</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

8. Web.xml中设置dwr支持

把dwr.jar放入依赖包路径中

14.web.xml中DWR的配置

<!-- web.xml DWR的配置需要dwr.jar包的支持 -->

<servlet>

<servlet-name>dwr-invoker</servlet-name>

<servlet-class>

org.directwebremoting.servlet.DwrServlet

</servlet-class>

<init-param>

<param-name>debug</param-name>

<param-value>true</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>dwr-invoker</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

9. Dwr独立管理Bean

15.WEB-INF/dwr.xml配置文件示例(必需设置)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC

"-//GetAhead Limited//DTD Direct Web Remoting2.0//EN"

"http://getahead.org/dwr/dwr20.dtd">

<dwr>

<allow>

<create creator="new" javascript="CountryHandle">

<param name="class" value="cn.qdqn.biz.CountryHandle"/>

</create>

</allow>

</dwr>

10.Dwr调用Spring管理的Bean

16.spring配置文件对dwr对象做注入(必需设置)

<!-- dwr对象注入 -->

<bean id="CountryHandle"class="cn.qdqn.dwr.CountryHandle">

<property name="daoServiceLocator" ref="DaoServiceLocator"></property>

</bean>

17.WEB-INF/dwr.xml配置文件中关于spring bean的调用(必需设置)

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC

"-//GetAhead Limited//DTD Direct Web Remoting2.0//EN"

"http://getahead.org/dwr/dwr20.dtd">

<dwr>

<allow>

<create creator="spring" javascript="CountryHandle">

<param name="beanName" value="CountryHandle" />

</create>

</allow>

</dwr>

整合中常见的问题及解决方法

11.使用Myeclipse整合时在工程中加入各框架的顺序

以下顺序可供参考

1. 新建web project

2. 加入Struts能力

3. 加入Spring能力

4. 加入Hibernate能力

5. 手工配置DWR

12.对数据库表进行hibernate自动生成代码时出错

原因可能为

1. 没有加入Hibernate支持

解法:对工程加入Hibernate支持

2. 由于asm包的冲突引发的

解法:参见问题 运行时报CGLIB出错

13.依赖注入时无法对基于类的属性进行set方式注入

原因是CGLIB包冲突参见问题运行时报CGLIB出错

14.运行时报CGLIB出错

原因是Spring和Hibernate都使用了asm和cglib两个库。

解决办法

.. 确保在加入包时不使用默认的引用库,而使用把包Copy到工程中的方法,通常在WEB-INF/lib下。

.. 去WEB-INF/lib文件夹下把

.. asm.jar

.. asm-attrs.jar

.. cglib-2.1.3.jar

三个包删除

.. 对工程进行刷新操作(选中工程名按F5)

2注意此方法以Myeclipse6.0为例,其它版本可能有所不同

Struts1.x Spring2.x Hibernate3.x DWR2.x 整合工具文档 第 10 页

15.Spring在加入时应该选哪几个包

如果Spring在hibernate之前加可以选如下几个包

.. Spring 2.0 AOP Libraries

.. Spring 2.0 Core Libraries

.. Spring 2.0 Web Libraries

如果Spring后加的话还要把持久化和Hibernate相关支持包选中(不推荐后加)。

16.Hibernate在加入时应该选哪几个包

如果Hibernate后加的话把所有出现的包都选中即可,如果按问题Spring在加入时应该选哪几个包的方法做这里应该出现三个:

.. Hibernate 3.1 Core libraries

.. Hibernate 3.1 Advanced support libraries

.. Spring 2.0 Persistence Core Libraries


热点排行