`

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

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- 配置spring的监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext*.xml</param-value>
    </context-param>
    <!-- 开启监听 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
    <filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <!-- 设置监听加载上下文 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
    </filter>
    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 applicationContext.xml源码:

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

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

<%@ 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>
                                <td>
                                    <s:property value="phone" />
                                </td>
                                <td>
                                    <s:property value="address" />
                                </td>
                                <td>
                                    <a
                                        href='<s:url action="edit"><s:param name="id" value="id" /></s:url>'>
                                        修改 </a> &nbsp;
                                    <a
                                        href='<s:url action="delete"><s:param name="id" value="id" /></s:url>'>
                                        删除 </a>
                                </td>
                            </tr>
                        </s:iterator>
                    </tbody>
                </table>
                <s:submit value="delete" />
            </s:form>
        </center>
    </body>
</html>
 

显示界面如图:

 

 

 

 

第一点注意 的是,搭建出项目,一定会报错,因为Spring 2.5 AOP Libraries中的asm的三个jar包会和

Hibernate 3.2 Core Libraries中的asm的jar包中的某些类中有冲突。所以一定要删除Spring中的三个asm的jar包。

第二点要注意 的是,struts2的配置包的导入,需要的是5个jar包分别是:

struts2-core-2.0.11.2.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

xwork-2.05.jar

commons-logging-1.0.4.jar

struts2+spring配置包:struts2-spring-plugin-2.0.11.2.jar

网上有些还说需要把4个spring的包拷到lib下,我是拷了但是,并不确定这样做是否有必要。

总之是正常运行了。

所以也就没想太多。如果有的朋友运行不了,可以考虑把这4个需要的包写上来。

 

 

 

 

 

 

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Struts2+Spring+Hibernate搭建全解

    ### Struts2+Spring+Hibernate框架搭建全解 在Java Web开发领域,Struts2、Spring与Hibernate(通常被简称为SSH)组合成一个非常流行的轻量级开发框架。这三者结合能够很好地解决Web应用中的业务逻辑处理、数据持久...

    Struts2+Spring+Hibernate搭建全解!

    接下来,需要整合Spring和Struts2,通过Struts2的Spring插件,让Struts2的动作类可以直接从Spring容器中获取依赖。这通常涉及到在Struts2的配置文件中添加Spring插件配置,以及在Action类上添加注解,声明其为Spring...

    newss2newss2newss2

    - - ITeye技术网站.mht":这是一个MHT(单一文件网页)文档,可能包含了详细的步骤和示例代码,指导开发者如何将Struts2、Spring和Hibernate整合在一起。通常,这类文档会涵盖配置文件的设置,如web.xml、struts....

    实训商业源码-咻一咻抽奖V4.3.1 开源版-毕业设计.zip

    实训商业源码-咻一咻抽奖V4.3.1 开源版-毕业设计.zip

    有线网调试demo源码和apk.zip

    有线网调试demo源码和apk.zip 有线网demo 的简单说明: 1、Android 原生没有有线网开关状态,需要自定义属性进行开关记忆;所以默认没设置switch开关控件; 2、有线网开关状态之前方案都是使用Settings.Global.ethernet 开关记录的,3588 AN15 使用的是prop属性记录; 3、静态ip设置:要和自动获取的ip在同一个网段;网关一般设置成X.X.X.1,设置错误会导致无法上网; 4、代理设置:端口号有范围:1-65535,以前好像遇到过311D2 wifi设置端口号范围超出数值会导致系统重启。 5、有些方案设置静态ip或者代理需要关开一次有线网才能生效。 6、有线网接入状态是导入了定制包的,只针对特定系统,其他系统不适用,直接使用估计会崩溃, 其他系统调试需要去除判断有线网接入部分代码,重新编译apk使用; 底层是如何实现判断有线网接入的,没有去研究分析,大概是通过io是否接入判断的。

    实训商业源码-掌上题库V1.2.2全开源版本-毕业设计.zip

    实训商业源码-掌上题库V1.2.2全开源版本-毕业设计.zip

    实训商业源码-梦昂图文10.2.14 公众号版-毕业设计.zip

    实训商业源码-梦昂图文10.2.14 公众号版-毕业设计.zip

    【优化控制】基于matlab遗传算法GA优化VTVL火箭姿态控制(串级PID控制结构 结合RK4动力学)【含Matlab源码 13267期】.zip

    985研究生,Matlab领域优质创作者 (1)如需代码 加腾讯企鹅号,见评论区或私信; (2)代码运行版本 Matlab 2019b (3)其他仿真咨询 1 完整代码包运行+运行有问题可咨询 2 期刊或论文复现; 3 程序定制; 4 期刊写作或指导; 5 科研合作;

    毕业论文-二次元应用下载页源码 带弹幕-整站商业源码.zip

    毕业论文-二次元应用下载页源码 带弹幕-整站商业源码.zip

    毕业论文-老虎-微信淘宝客V6.0.7-整站商业源码.zip

    毕业论文-老虎-微信淘宝客V6.0.7-整站商业源码.zip

    毕业论文-方熊表单V1.0.0 开源版-整站商业源码.zip

    毕业论文-方熊表单V1.0.0 开源版-整站商业源码.zip

    利用深度学习图像分割UNet技术的肿瘤区域识别与辅助诊断系统Python代码及全套数据(优质项目)

    基于深度学习图像分割Unet的肿瘤区域识别辅助诊断系统的Python源码及全部数据(高分项目)。该项目经导师指导并认可,获得98分,适合计算机相关专业学生进行课程设计、期末大作业或项目实战练习。。内容来源于网络分享,如有侵权请联系我删除。

    Python微信关键词自动回复

    内容概要 本文介绍了一个用 Python 编写的微信自动回复程序。通过安装特定依赖,配置config.xlsx文件中的关键词和回复内容,运行auto_reply.py程序,即可实现微信自动回复功能。程序会持续监测微信新消息,满足特定条件时,依据关键词匹配回复内容进行自动回复。 适用人群 适合微信消息较多,希望节省回复时间的人群,尤其是经常被重复性消息打扰的办公族、客服人员等,也适合对 Python 编程感兴趣,想要尝试开发简单自动化工具的初学者。 使用场景及目标 在工作场景中,能自动回复常见问题,提高沟通效率;生活里,可快速回应亲友的常用问候,避免遗漏消息。目标是减少手动回复的繁琐,解放双手,让信息处理更高效。 其他说明 运行前需确保正确安装依赖,可修改config.xlsx来自定义回复规则。运行程序后,按Ctrl+C可停止。若遇到问题,可从依赖安装、文件配置等方面排查。

    实训商业源码-全新开源贫穷网打赏源码-毕业设计.zip

    实训商业源码-全新开源贫穷网打赏源码-毕业设计.zip

    实训商业源码-智云物业2.3.9-毕业设计.zip

    实训商业源码-智云物业2.3.9-毕业设计.zip

    CAD技术在农业机械工程设计中的运用研究.pdf

    CAD技术在农业机械工程设计中的运用研究.pdf

    【AI应用开发】基于Dify平台的大型语言模型应用构建与优化:从安装到实战案例详解

    内容概要:本文介绍了Dify——一个用于开发大型语言模型(LLM)应用程序的开源平台。Dify融合了后端即服务(BaaS)和LLMOps理念,使开发者能快速构建生产级别的生成式AI应用。它支持多种LLM模型,包括GPT、Mistral、Llama3等,并兼容多种推理提供商。Dify内置了高质量的检索增强生成(RAG)引擎和灵活的Agent框架,支持聊天助手、文本生成、Agent应用和工作流等多种应用类型。通过丰富的功能组件,如数据集管理、可视化Prompt编排、应用运营工具和插件生态系统,Dify极大简化了AI应用的开发过程。文章还展示了Dify在电商智能客服、新媒体内容生成和企业办公自动化等实际场景中的应用案例,并与FastGPT进行了对比,突出了Dify在模型接入、应用构建和用户友好度等方面的优势。 适合人群:对AI应用开发感兴趣的研发人员,尤其是希望快速构建和部署AI应用的开发者和企业。 使用场景及目标:①通过Dify的强大模型支持和RAG引擎,快速构建智能客服、内容生成等AI应用;②利用Agent框架和工作流功能,实现复杂任务的自动化处理;③通过丰富的功能组件和插件生态系统,提升应用的灵活性和功能性。 其他说明:Dify不仅提供了便捷的安装和使用指南,还展望了未来的发展前景,强调其在降低AI应用开发门槛和推动AI技术创新方面的巨大潜力。

    实训商业源码-深蓝AI智能名片小程序1.7.1-毕业设计.zip

    实训商业源码-深蓝AI智能名片小程序1.7.1-毕业设计.zip

    【研华科技】以数智融合打造可持续管理的企业未来.pdf

    【研华科技】以数智融合打造可持续管理的企业未来.pdf

    “平台+生态”打造产业数字化新引擎.pdf

    “平台+生态”打造产业数字化新引擎.pdf

Global site tag (gtag.js) - Google Analytics