热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Struts2+Sring+Hibernate简单配置

2019独角兽企业重金招聘Python工程师标准Struts2SpringHibernate搭建全解!Struts2SpringHibernate是J2EE的最

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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"
>
< struts >

    
< constant  name &#61;"struts.objectFactory"  value &#61;"spring" />
    

    
< package  name &#61;"crm_employee"  extends &#61;"struts-default"  namespace &#61;"/emp" >
        
< action  name &#61;"add"  class &#61;"addBean"  method &#61;"add" >
            
< result > add.action result >
            
< result > /emp/add_suc.jsp result >
        
action >
        
< action  name &#61;"list"  class &#61;"listBean"  method &#61;"list" >
            
< result > /emp/list.jsp result >
        
action >
        
< action  name &#61;"delete"  class &#61;"deleteBean"  method &#61;"delete" >
            
< result > delete.action result >
            
< result > /emp/delete_suc.jsp result >
        
action >
        
< action  name &#61;"update"  class &#61;"updateBean"  method &#61;"update" >
            
< result > update.action result >
            
< result > /emp/edit_suc.jsp result >
        
action >
        
< action  name &#61;"edit"  class &#61;"editBean"  method &#61;"edit" >
            
< result > /emp/edit.jsp result >
        
action >
        

    
package >
struts >

 

web.xml源码&#xff1a;

xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
< web-app  version &#61;"2.5"  xmlns &#61;"http://java.sun.com/xml/ns/javaee"
    xmlns:xsi
&#61;"http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
&#61;"http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    

    
< 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 >
    

    
< 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源码&#xff1a;

xml version&#61;"1.0" encoding&#61;"UTF-8" ?>
< beans  xmlns &#61;"http://www.springframework.org/schema/beans"
    xmlns:xsi
&#61;"http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop
&#61;"http://www.springframework.org/schema/aop"
    xmlns:tx
&#61;"http://www.springframework.org/schema/tx"
    xsi:schemaLocation
&#61;"
    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"
>
    

    
< bean  id &#61;"dataSource"
        class
&#61;"org.apache.commons.dbcp.BasicDataSource" >
        
< property  name &#61;"driverClassName"
            value
&#61;"com.mysql.jdbc.Driver" >
        
property >
        
< property  name &#61;"url"
            value
&#61;"jdbc:mysql://localhost:3306/tables" >
        
property >
        
< property  name &#61;"username"  value &#61;"root" > property >
        
< property  name &#61;"password"  value &#61;"hicc" > property >
    
bean >
    
< bean  id &#61;"sessionFactory"
        class
&#61;"org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name &#61;"dataSource" >
            
< ref  bean &#61;"dataSource"   />
        
property >
        
< property  name &#61;"hibernateProperties" >
            
< props >
                
< prop  key &#61;"hibernate.dialect" >
                    org.hibernate.dialect.MySQLDialect
                
prop >
                
< prop  key &#61;"hibernate.show_sql" > true prop >
            
props >
        
property >
        
< property  name &#61;"mappingResources" >
            
< list >
                
< value > com/sy/crm/model/Employee.hbm.xml value >
            
list >
        
property >
    
bean >
    
< bean  id &#61;"employeeDao"
        class
&#61;"com.sy.crm.dao.hibernate.EmployeeDaoHibernate" >
        
< property  name &#61;"sessionFactory" >
            
< ref  bean &#61;"sessionFactory"   />
        
property >
    
bean >
    
< bean  id &#61;"employeeManager"
        class
&#61;"com.sy.crm.service.impl.EmployeeManagerImpl" >
        
< property  name &#61;"employeeDao" >
            
< ref  bean &#61;"employeeDao"   />
        
property >
    
bean >
    
    
< bean  id &#61;"addBean"  class &#61;"com.sy.crm.action.EmployeeAction"  scope &#61;"prototype" >
        
< property  name &#61;"employeeManager" >
            
< ref  bean &#61;"employeeManager"   />
        
property >
    
bean >
    
< bean  id &#61;"listBean"  class &#61;"com.sy.crm.action.EmployeeAction"  scope &#61;"prototype" >
        
< property  name &#61;"employeeManager" >
            
< ref  bean &#61;"employeeManager"   />
        
property >
    
bean >
    
< bean  id &#61;"deleteBean"  class &#61;"com.sy.crm.action.EmployeeAction"  scope &#61;"prototype" >
        
< property  name &#61;"employeeManager" >
            
< ref  bean &#61;"employeeManager"   />
        
property >
    
bean >
    
< bean  id &#61;"updateBean"  class &#61;"com.sy.crm.action.EmployeeAction"  scope &#61;"prototype" >
        
< property  name &#61;"employeeManager" >
            
< ref  bean &#61;"employeeManager"   />
        
property >
    
bean >
    
< bean  id &#61;"editBean"  class &#61;"com.sy.crm.action.EmployeeAction"  scope &#61;"prototype" >
        
< property  name &#61;"employeeManager" >
            
< ref  bean &#61;"employeeManager"   />
        
property >
    
bean >
    

    
< bean  id &#61;"transactionManager"  
    class
&#61;"org.springframework.orm.hibernate3.HibernateTransactionManager" >
    
< property  name &#61;"sessionFactory" >
    
< ref  local &#61;"sessionFactory" />
    
property >
    
bean >
    

    
< tx:advice  id &#61;"txAdvice"  transaction-manager &#61;"transactionManager" >
    
< tx:attributes >
    
< tx:method  name &#61;"add*"  propagation &#61;"REQUIRED" />
    
< tx:method  name &#61;"delete*"  propagation &#61;"REQUIRED" />
    
< tx:method  name &#61;"update*"  propagation &#61;"REQUIRED" />
    
< tx:method  name &#61;"*"  read-only &#61;"true" />
    
tx:attributes >
    
tx:advice >
    

    
< aop:config >
    
< aop:pointcut  id &#61;"allManagerMethod"  expression &#61;"execution(*
    com.sy.crm.service.*.*(..))"
/>
    
< aop:advisor  advice-ref &#61;"txAdvice"  pointcut-ref &#61;"allManagerMethod" />
    
aop:config >
    
beans >
add.jsp源码&#xff1a;
<% &#64; page language &#61; " java "  pageEncoding &#61; " utf-8 " %>
<% &#64; taglib uri &#61; " /struts-tags "  prefix &#61; " s "   %>
DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
  
< head >
    
< title > add page title >
    
< script language &#61; " Javascript "  src &#61; " validation-framework.js " > script >
    
< meta http - equiv &#61; " pragma "  content &#61; " no-cache " >
    
< meta http - equiv &#61; " cache-control "  content &#61; " no-cache " >
    
< meta http - equiv &#61; " expires "  content &#61; " 0 " >     
    
< meta http - equiv &#61; " keywords "  content &#61; " keyword1,keyword2,keyword3 " >
    
< meta http - equiv &#61; " description "  content &#61; " This is my page " >

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

list.jsp源码&#xff1a;

 

<% &#64; page language &#61; " java "  pageEncoding &#61; " utf-8 " %>
<% &#64; taglib uri &#61; " /struts-tags "  prefix &#61; " s " %>
DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
    
< head >
        
< title > list employee page title >

        
< meta http - equiv &#61; " pragma "  content &#61; " no-cache " >
        
< meta http - equiv &#61; " cache-control "  content &#61; " no-cache " >
        
< meta http - equiv &#61; " expires "  content &#61; " 0 " >
        
< meta http - equiv &#61; " keywords "  content &#61; " keyword1,keyword2,keyword3 " >
        
< meta http - equiv &#61; " description "  content &#61; " This is my page " >
        
< style type &#61; " 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 >
                雇员管理&#xff1a;
            
h3 >
            
< br >
            
< h4 >
                
< a href &#61; " ../emp/add.jsp " > 员工注册 a >
            
h4 >
            
< s:form action &#61; " delete "  theme &#61; " simple " >
                
< table >
                    
< thead >
                        
< tr >
                            
< th >
                                选择
                            
th >
                            
< th >
                                编号
                            
th >
                            
< th >
                                姓名
                            
th >
                            
< th >
                                电话
                            
th >
                            
< th >
                                地址
                            
th >
                            
< th >
                                操作
                            
th >
                        
tr >
                    
thead >
                    
< tbody >
                        
< s:iterator value &#61; " employees " >
                            
< tr >
                                
< td >
                                    
< input type &#61; " checkbox "  name &#61; " id "
                                        value
&#61; &#39; &#39;   />
                                
td >
                                
< td >
                                    
< s:property value &#61; " id "   />
                                
td >
                                
< td >
                                    
< s:property value &#61; " name "   />
                                
td >
                                
< td >
                                    
< s:property value &#61; " phone "   />
                                
td >
                                
< td >
                                    
< s:property value &#61; " address "   />
                                
td >
                                
< td >
                                    
< a
                                        href
&#61; &#39; &#39; >
                                        修改 
a >   & nbsp;
                                    
< a
                                        href
&#61; &#39; &#39; >
                                        删除 
a >
                                
td >
                            
tr >
                        
s:iterator >
                    
tbody >
                
table >
                
< s:submit value &#61; " delete "   />
            
s:form >
        
center >
    
body >
html >

转:https://my.oschina.net/u/729917/blog/149742



推荐阅读
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • Hibernate延迟加载深入分析-集合属性的延迟加载策略
    本文深入分析了Hibernate延迟加载的机制,特别是集合属性的延迟加载策略。通过延迟加载,可以降低系统的内存开销,提高Hibernate的运行性能。对于集合属性,推荐使用延迟加载策略,即在系统需要使用集合属性时才从数据库装载关联的数据,避免一次加载所有集合属性导致性能下降。 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • 本文介绍了禅道作为一款国产开源免费的测试管理工具的特点和功能,并提供了禅道的搭建和调试方法。禅道是一款B/S结构的项目管理工具,可以实现组织管理、后台管理、产品管理、项目管理和测试管理等功能。同时,本文还介绍了其他软件测试相关工具,如功能自动化工具和性能自动化工具,以及白盒测试工具的使用。通过本文的阅读,读者可以了解禅道的基本使用方法和优势,从而更好地进行测试管理工作。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • Asp.net Mvc Framework 七 (Filter及其执行顺序) 的应用示例
    本文介绍了在Asp.net Mvc中应用Filter功能进行登录判断、用户权限控制、输出缓存、防盗链、防蜘蛛、本地化设置等操作的示例,并解释了Filter的执行顺序。通过示例代码,详细说明了如何使用Filter来实现这些功能。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
author-avatar
手机用户2502920971
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有