传智Struts2笔记(15)Struts2+Spring2.5+Hibernate3.3整合开发
[b]Struts2+Spring2.5+Hibernate3.3整合开发[/b] [color=red]下面给出整合开发时Struts 2、 Hibernate、Spring需要的JAR。[/color] struts2-core-2.x.x.jar :Struts 2框架的核心类库 Xwork-core-2.x.x.jar :XWork类库,Struts 2在其上构建 ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性 freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写 commons-fileupload-1.2.x.jar 文件上传组件,2.1.6版本后需要加入此文件 struts2-spring-plugin-2.x.x.jar :用于struts2集成Spring的插件 [color=blue]hibernate核心安装包下的[/color](下载路径:http://www.hibernate.org/,点击“Hibernate Core”右边的“Downloads”): hibernate3.jar lib\bytecode\cglib\hibernate-cglib-repack-2.1_3.jar lib\required\*.jar [color=blue]hibernate 注解安装包下的[/color](下载路径:www.hibernate.org,点击“Hibernate Annotations”右边的“Downloads”): hibernate-annotations.jar lib\ejb3-persistence.jar、hibernate-commons-annotations.jar [color=blue]Hibernate针对JPA的实现包[/color](下载路径:www.hibernate.org,点击“Hibernate Entitymanager”右边的“Downloads”): hibernate-entitymanager.jar lib\test\log4j.jar、slf4j-log4j12.jar [color=blue]Spring安装包下的[/color] dist\spring.jar lib\c3p0\c3p0-0.9.1.2.jar lib\aspectj\aspectjweaver.jar、aspectjrt.jar lib\cglib\cglib-nodep-2.1_3.jar lib\j2ee\common-annotations.jar lib\log4j\log4j-1.2.15.jar lib\jakarta-commons\commons-logging.jar [color=blue]MYSQL数据库驱动jar[/color] [color=red]Spring的配置模版:[/color] [code="xml"] [/code] [b]第一步在Spring中配置数据源:[/b] [code="xml"] [/code] [b]第二步集成进hibernate :[/b] [code="xml"] cn/itcast/bean/buyer.hbm.xml hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false [/code] [b]第三步使用Spring容器管理事务服务:[/b] [code="xml"] [/code] [b]配置hibernate实体映射文件buyer.hbm.xml:[/b] [code="xml"] cn.itcast.bean.Gender 12 [/code] [b]在web容器中使用Listener实例化spring容器和配置struts2[/b] !-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 --> [code="xml"] contextConfigLocation classpath:beans.xml org.springframework.web.context.ContextLoaderListener [/code] [b]配置struts2[/b] [code="xml"] struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* [/code] [b]struts2的配置文件模版struts.xml如下。常量struts.objectFactory=spring明确指出将由Spring负责创建Action实例。[/b] [code="xml"] /WEB-INF/page/message.jsp /WEB-INF/page/persons.jsp [/code] [color=red] [/color] [color=green]为了能从spring容器中寻找到Action bean,要求action配置的class属性值和spring中bean的名称相同。[/color] [b]使用spring解决struts2乱码问题。[/b] [code="xml"] encoding org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 encoding /* [/code] [b]使用spring解决hibernate因session关闭导致的延迟加载例外问题。[/b] [code="xml"] OpenSessionInViewFilter org.springframework.orm.hibernate3.support.OpenSessionInViewFilter sessionFactoryBeanName sessionFactory OpenSessionInViewFilter /* [/code] [b]struts2的标签[/b] <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> 姓名:
, 修改