Struts2+Spring+Hibernate搭建全解.docx
- 文档编号:2222903
- 上传时间:2023-05-02
- 格式:DOCX
- 页数:13
- 大小:125.57KB
Struts2+Spring+Hibernate搭建全解.docx
《Struts2+Spring+Hibernate搭建全解.docx》由会员分享,可在线阅读,更多相关《Struts2+Spring+Hibernate搭建全解.docx(13页珍藏版)》请在冰点文库上搜索。
Struts2+Spring+Hibernate搭建全解
Struts2+Spring+Hibernate是J2EE的最新流行框架。
本篇是我搭建这个框架的经验总结,有很多人搭建这个框架总会遇到
大大小小的问题,网上也没有什么行之有效的方案或成体系的介绍,所以我就决定总结一下我的搭建过程。
给一些搭
建尚存问题的朋友提供帮助。
我用这个框架,实现的是基本的CRUD功能的一个雇员管理系统,本来打算丰富一下功能,但是一直没能抽出空去搞。
目前版本暂定为1.0,除了CRUD外还配置了表单验证框架JSValidation。
功能都能很顺利的实现。
现在分享部分源码,来说明一些注意事项。
以下是部分搭建过程及源码:
1.先组合实现Hibernate3.2+Spring2.5支持,删除hibernate.cfg.xml文件,修改applicationContext.xml文件的内容,增加SessionFactory和dataSource的设置。
2.通过MyEclipse的向导方式,生成POJO类和对应的映射文件。
3.修改applicationContext.xml文件中
4.编写DAO接口和实现类。
5.修改applicationContext.xml文件,增加对Dao实现类的配置。
6.组合Struts2和Spring2.5,修改web.xml文件,增加struts2的所需要的过滤器配置。
7.增加struts2相应类库,增加struts2与spring的配置jar包。
8.拷贝struts.xml文件到src根目录下,再修改struts.xml文件,进行常量配置。
9.修改web.xml文件,配置Spring监听器,和上下文变量。
并增加OpenSessionInViewFilter的设置。
10.写入action类。
11.配置struts.xml文件。
12.修改applicationContext.xml
13.编写Jsp文件。
14.加载运行项目。
下面是关键文件的源码:
struts.xml源码:
xml version="1.0" encoding="UTF-8"?
>
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http:
//struts.apache.org/dtds/struts-2.0.dtd">
-- struts2委托spring管理 -->
-- /crm/emp/add.action -->
-- Add actions here -->
web.xml源码:
xml version="1.0" encoding="UTF-8"?
>
xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation=" -- 配置spring的监听器 --> -- 开启监听 --> org.springframework.web.context.ContextLoaderListener -- 配置OpenSessionInViewFilter,必须在struts2监听之前 --> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter -- 设置监听加载上下文 --> org.apache.struts2.dispatcher.FilterDispatcher
applicationContext.xml源码:
xml version="1.0" encoding="UTF-8"?
>
//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.5.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"> -- 配置Hibernate支持 --> class="mons.dbcp.BasicDataSource"> value="com.mysql.jdbc.Driver"> value="jdbc: mysql: //localhost: 3306/tables"> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> org.hibernate.dialect.MySQLDialect class="com.sy.crm.dao.hibernate.EmployeeDaoHibernate"> class="com.sy.crm.service.impl.EmployeeManagerImpl"> -- 事务管理器 --> class="org.springframework.orm.hibernate3.HibernateTransactionManager"> -- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required --> advice id="txAdvice" transaction-manager="transactionManager"> attributes> method name="add*" propagation="REQUIRED"/> method name="delete*" propagation="REQUIRED"/> method name="update*" propagation="REQUIRED"/> method name="*" read-only="true"/> attributes> advice> -- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包, 类中所有方法需要,还需要参考tx: advice的设置 --> config> pointcut id="allManagerMethod" expression="execution(* com.sy.crm.service.*.*(..))"/> advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/> config>
add.jsp源码:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
雇员注册:
查看所有雇员
blue; font-weight:
bold;">
form action="add" method="post" onsubmit="return doValidate('form')" name="form" id="form"> textfield name="employee.name" label="姓名" id="name"/> textfield name="employee.address" label="地址"/> textfield name="employee.phone" label="电话"/> submit value="员工注册"/> form>
list.jsp源码:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
table {
border:
1px solid black;
border-collapse:
collapse;
}
table thead tr th {
border:
1px solid black;
padding:
3px;
background-color:
#cccccc;
}
table tbody tr td {
border:
1px solid black;
padding:
3px;
}
雇员管理:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Struts2 Spring Hibernate 搭建