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文件中<property name="mappingResources">元素的内容。

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源码:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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"
>
<struts>
<!-- struts2委托spring管理 -->
    
<constant name="struts.objectFactory" value="spring"/>
    
<!-- /crm/emp/add.action -->
    
<package name="crm_employee" extends="struts-default" namespace="/emp">
        
<action name="add" class="addBean" method="add">
            
<result>add.action</result>
            
<result>/emp/add_suc.jsp</result>
        
</action>
        
<action name="list" class="listBean" method="list">
            
<result>/emp/list.jsp</result>
        
</action>
        
<action name="delete" class="deleteBean" method="delete">
            
<result>delete.action</result>
            
<result>/emp/delete_suc.jsp</result>
        
</action>
        
<action name="update" class="updateBean" method="update">
            
<result>update.action</result>
            
<result>/emp/edit_suc.jsp</result>
        
</action>
        
<action name="edit" class="editBean" method="edit">
            
<result>/emp/edit.jsp</result>
        
</action>
        
<!-- Add actions here -->
    
</package>
</struts>

 

web.xml源码:

applicationContext.xml源码:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?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.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支持 -->
    
<bean id="dataSource"
        class
="org.apache.commons.dbcp.BasicDataSource">
        
<property name="driverClassName"
            value
="com.mysql.jdbc.Driver">
        
</property>
        
<property name="url"
            value
="jdbc:mysql://localhost:3306/tables">
        
</property>
        
<property name="username" value="root"></property>
        
<property name="password" value="hicc"></property>
    
</bean>
    
<bean id="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource">
            
<ref bean="dataSource" />
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                
</prop>
                
<prop key="hibernate.show_sql">true</prop>
            
</props>
        
</property>
        
<property name="mappingResources">
            
<list>
                
<value>com/sy/crm/model/Employee.hbm.xml</value>
            
</list>
        
</property>
    
</bean>
    
<bean id="employeeDao"
        class
="com.sy.crm.dao.hibernate.EmployeeDaoHibernate">
        
<property name="sessionFactory">
            
<ref bean="sessionFactory" />
        
</property>
    
</bean>
    
<bean id="employeeManager"
        class
="com.sy.crm.service.impl.EmployeeManagerImpl">
        
<property name="employeeDao">
            
<ref bean="employeeDao" />
        
</property>
    
</bean>
    
    
<bean id="addBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        
<property name="employeeManager">
            
<ref bean="employeeManager" />
        
</property>
    
</bean>
    
<bean id="listBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        
<property name="employeeManager">
            
<ref bean="employeeManager" />
        
</property>
    
</bean>
    
<bean id="deleteBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        
<property name="employeeManager">
            
<ref bean="employeeManager" />
        
</property>
    
</bean>
    
<bean id="updateBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        
<property name="employeeManager">
            
<ref bean="employeeManager" />
        
</property>
    
</bean>
    
<bean id="editBean" class="com.sy.crm.action.EmployeeAction" scope="prototype">
        
<property name="employeeManager">
            
<ref bean="employeeManager" />
        
</property>
    
</bean>
    
<!-- 事务管理器 -->
    
<bean id="transactionManager" 
    class
="org.springframework.orm.hibernate3.HibernateTransactionManager">
    
<property name="sessionFactory">
    
<ref local="sessionFactory"/>
    
</property>
    
</bean>
    
<!-- 配置事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
    
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    
<tx:attributes>
    
<tx:method name="add*" propagation="REQUIRED"/>
    
<tx:method name="delete*" propagation="REQUIRED"/>
    
<tx:method name="update*" propagation="REQUIRED"/>
    
<tx:method name="*" read-only="true"/>
    
</tx:attributes>
    
</tx:advice>
    
<!-- 配置那些类的方法进行事务管理,当前com.sy.crm.service包中的子包,
    类中所有方法需要,还需要参考tx:advice的设置 
-->
    
<aop:config>
    
<aop:pointcut id="allManagerMethod" expression="execution(*
    com.sy.crm.service.*.*(..))"
/>
    
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    
</aop:config>
    
</beans>
 
add.jsp源码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
<title>add page</title>
    
<script language="JavaScript" src="validation-framework.js"></script>
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">

  
</head>
  
<body>
  
<center>
   
<h3>雇员注册:</h3><br>
   
<h4><a href="../emp/list.action">查看所有雇员</a></h4>
   
<div id="error" style="color:blue; font-weight:bold;"></div>
   
<s:form action="add" method="post" onsubmit="return doValidate('form')" name="form" id="form">
   
<s:textfield name="employee.name" label="姓名" id="name"/>
   
<s:textfield name="employee.address" label="地址"/>
   
<s:textfield name="employee.phone" label="电话"/>
   
<s:submit value="员工注册"/>
   
</s:form>
   
</center>
  
</body>
</html>

list.jsp源码:

 

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    
<head>
        
<title>list employee page</title>

        
<meta http-equiv="pragma" content="no-cache">
        
<meta http-equiv="cache-control" content="no-cache">
        
<meta http-equiv="expires" content="0">
        
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        
<meta http-equiv="description" content="This is my page">
        
<style type="text/css">
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;
}
</style>
    
</head>

    
<body>
        
<center>
            
<h3>
                雇员管理:
            
</h3>
            
<br>
            
<h4>
                
<a href="../emp/add.jsp">员工注册</a>
            
</h4>
            
<s:form action="delete" theme="simple">
                
<table>
                    
<thead>
                        
<tr>
                            
<th>
                                选择
                            
</th>
                            
<th>
                                编号
                            
</th>
                            
<th>
                                姓名
                            
</th>
                            
<th>
                                电话
                            
</th>
                            
<th>
                                地址
                            
</th>
                            
<th>
                                操作
                            
</th>
                        
</tr>
                    
</thead>
                    
<tbody>
                        
<s:iterator value="employees">
                            
<tr>
                                
<td>
                                    
<input type="checkbox" name="id"
                                        value
='<s:property value="id" />' />
                                
</td>
                                
<td>
                                    
<s:property value="id" />
                                
</td>
                                
<td>
                                    
<s:property value="name" />
                                
</td>
                     
精通SSH框架:Struts2+Spring2+Hibernate3综合指南 本文还有配套的精品资源,点击获取 简介:Struts2、Spring2 和 Hibernate3 组合形成了 Java Web 开发中的“SSH”三位一体,提供了完整的模型-视图-控制器(MVC)架构、事务管理和数据持久化解决方案。本课程旨在介绍这三个框架的核心概念、配置方法和在企业级应用中的协同工作方式,帮助开发者提升开发效率和系统的可维护性。 1. Strut... 阅读详情

相关推荐

SSH(Spring+Struts2+Hibernate)整合步骤

SSH (Spring+Struts2+Hibernate)整合步骤纯手写

Spring+Struts2+hibernate整合

SSH三大框架整合(1)struts2(2)hibernate5.x(3)spring4.x回顾SSH框架知识点Hibernate框架1、 hibernate核心配置文件(1)orm思想- 对象关系映射(1)数据库信息(2)hibernate信息(3)映射配置(4)hibernate核心配置文件- 如果单纯使用hibernate框架,核心配置文件名称 hibernate.cfg.xml和位置 sr...

m0_37307255的博客 2780

Spring+Struts2+Hibernate整合JAR包

Spring+Struts2+Hibernate整合JAR包,外加一个注解包提供代码注解

Struts2+spring+hibernate整合

第四章 搭建SSH框架 本章目标 掌握Spring与Struts 2的集成重点(难点) 掌握Spring与Hibernate的集成重点(难点) SSH系统程序架构 SSH架构 Struts 2 + Spring + Hibernate 以Spring作为核心框架,数据持久化使用Hibernate完成,表现层使用Struts 2 Spring提供对象管理、面向切面编程等实用功能 通过...

Jaosn.Yang的博客 2895

ssh2整合(spring+struts2+hibernate)-附

NULL 博文链接:https://blessht.iteye.com/blog/981071

struts2+spring+hibernate整合示例

SSH整合示例(详情见我博客专栏)之前的博客我们总结了spring基础、spring分别整合struts2、hibernate、mybatis等,今天我们来同时整合下 struts、spring、hibernate,也就是所谓的 ssh 。 整合流程: 1 首先整合spring和hibernate,这次我们在spring 中配置bean使用注解的方式 ,hibernate实体映射关系也使用注解的方式,配置完毕后用简单方法测试下hibernate是否整合成功。 a 加入支持:添加 spring核心包、hibernate 3.6 包、 spring整合hibernate包 , 在src下建立applicationContext.xml (先建立src下便于测试hibernate)。 b 编写实体类,加入hibernate注解,编写方法类测试类,在applicationContext.xml中添加hibernate模板类配置以及包扫描语句 。在类中添加spring bean注解。 c 测试类中 主动解析applicationContext.xml ,获取bean 执行dao层方法进行测试 2 将struts2 整合进去, 这次在struts.xml中我们使用通配符的方式配置action。 a 加入支持 : 添加struts2.3.15 必需包 以及 struts json包(ajax要用到),spring整合struts2包,spring web 包,在src目录下建立struts.xml,复制头文件进去。将applicationContext.xml移到WEB-INF目录下。web容器中(web.xml)中添加struts2 filter以及spring 监听器。 b 在struts.xml中添加action,使用通配符的方式 , 注意这里和单独struts2不同的地方: class属性指向的是bean 的 id ,这里我们配置bean采用spring ioc注解的方式, 所以默认的bean的id 为 类名(首字母小写) c 编写action类、页面进行测试

(三大框架SSH)面试题锦集

Java工程师(程序员)面题 Struts,Spring,Hibernate三大框架    1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory3.打开Session 4.创建事务Transation 5.持久化操作 6.提交事务 7.关闭Session 8.关闭SesstionFactory 为什么要

maomao092092的专栏 7万+

详解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)

主要介绍了详解JAVAEE——SSH三大框架整合(spring+struts2+hibernate),具有一定的参考价值,感兴趣的小伙伴们可以参考一下

上一篇: Oracle BIEE 中的时间维与时间轴函数
下一篇: SVN协同开发配置方案!
ghost_fly
博客等级 码龄20年 5粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值