您的位置:首页 > 编程语言 > Java开发

struts2.0与spring2.0的整合 spring和hibernate的整合

2010-04-22 14:48 369 查看
一、struts2.0与spring2.0的整合

1、新建web项目

加入struts2.0和spring的各种架包。

struts2.0的基本架包:

common-logging-1.0.4.jar freemarker-2.3.8.jar

ognl-2.6.11.jar struts2-core-2.0.11.2.jar xwork-2.0.5.jar

spring 的架包:spring-2.0.jar

特别的注意加入:

commons-pool-1.4.jar 解决spring和hibernate整合时,数据源datasource的问题,可以去http://commons.apache.org/downloads/download_pool.cgi下载

struts2-spring-plugin-2.0.11.2.jar 解决struts2和spring整合时,将struts的管理托管给spring

2、修改web.xml文件,加入:

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ActionContextCleanUp
</filter-class>
</filter>

上面一段是让程序知道在正确的时候清除请求(servletContext),而不是立即清除,方便在程序中取得servletContext。

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

上面一段是配置spring 的监听,方便spring与web的容器交互。

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

上面是定义spring.xml文件的上下文配置,如果拥有多个spring的配置文件,可以用空格或逗号隔开。

3、修改struts.xml文件

在其中加入:

<constant name="struts.objectFactory" valus="spring"/>

将struts的工厂交由spring来管理,由spring来创建对象,但是如果已经加入了struts2-spring-plugin-2.0.11.2.jar 的包,就可以不用加了。

二、spring和hibernate的整合

1、新建一web项目,或是在struts+spring的基础上开发

2、添加spring的支持

选择需要的库文件。aop、core、persistence core、presistence JDBC、web,选择这五个库文件。

3、添加hibernate的支持

在选择hibernate的配置文件时,我们选择spring configuration file ,表示下网将hibernate托管给spring进行管理,使用已经存在的spring的配置文件,检查spring的配置文件的位置,添加sessionFactory的BeanID,在下面给出的datasource数据源的BeanID,选择数据库的驱动名字,不创建sessionfactory class。

4、添加hibernate的反向工程

在myeclipse database explorer视图中,将表反转到hibernate中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: