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个需要的包写上来。

好了就说这么多吧,有疑问的朋友可以留言。我会尽量答复。

施杨出品!!!

 

分类:  (-J2EE Web-)
14
0
(请您对文章做出评价)
« 上一篇: SVN协同开发配置方案!
» 下一篇: 轻松Ghost XP系统!
posted on  2008-09-27 22:49  施杨 阅读( 99059) 评论( 75编辑  收藏
< Prev 1 2
#51楼   2009-12-19 08:38 |  haoxinren[未注册用户]
楼主 能发给我一份源码吗?期待中
com.software@163.com

#52楼   2009-12-23 16:24 |  dffdf[未注册用户]
sadfsdf
#53楼   2009-12-23 16:27 |  javanewer[未注册用户]
楼主能给我也发一份源码吗?
specialfw_521@163.com
#54楼   2011-01-12 10:46 |  深灰   
struts2怎么还用FilterDispatcher?
#55楼   2011-04-20 09:31 |  xizhaohui   
楼主给我一份源码吧,谢谢。zhao_hui_xi@hotmail.com
#56楼   2011-06-28 11:58 |  wang27   
能不能把源码发一份给我, 谢谢. wangdexheng27@yahoo.cn
#57楼   2011-10-28 17:59 |  seczhu   
给我发一份源码吧楼主、学习了!谢谢了!63230860@163.com
#58楼   2011-11-22 14:36 |  _Xmao   
谢谢分享, 不知道楼主现在还看不看博客. 最近刚刚看到您这篇文章. 想学习下, 希望楼主可以把源代码发给我一下...(不知道还有没有...) 谢谢楼主...
我的邮箱: zhai.xianglu@gmail.com
#59楼 [ 楼主2011-11-22 15:22 |  施杨   
抱歉了,这是我三年前的文章,代码不大好找了,文中贴了几乎所有的关键代码,希望能够提供帮助。
#60楼   2011-11-29 13:14 |  周雷   
引用 阿斯顿飞:就你这点水平来显摆什么,这是.net社区,不要来瞎折腾

未注册用户怎么知道这里是.net社区?见过2的没见过这么2的,这里是个人博客,发什么是个人自由,不懂可以不看,#$%#$%#$%
#61楼   2011-12-02 16:27 |  Jason Lee 001   
好文章可以经得起时间考验,楼主请发源码给我好吗?谢谢~
fordsupr@sina.cn
#62楼   2011-12-09 18:17 |  avenxia   
新手支持开源~!
#63楼   2011-12-12 09:19 |  yovii   
您好楼主和各位高人!我现在使用的spring+webwork+hibernate搭建的框架,依然出现楼上说的点击action出现空指针现象,很疑惑原因所在,感谢各位提点;
xwork.xml
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN"
" http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml"></include>
<package name="default" extends="webwork-default">
<action name="login" class="action.UserAction" method="login" >
<result name="success">/ok.jsp</result>
<result name="error">/err.jsp</result>
</action>
</package>
</xwork>
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">
<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/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></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>
</props>
</property>
<property name="mappingResources">
<list>
<value>vo/Users.hbm.xml</value>
<value>vo/Result.hbm.xml</value></list>
</property></bean>

<bean id="BaseDao" class="dao.BaseDao" >
<property name="sessionFactory">
<ref bean="seesionfactory" />
</property>
</bean>
<bean id="UserDao" class="dao.impl.UserDao"
parent="BaseDao">
</bean>
<bean id="userservice" class="service.impl.UserService">
<property name="userdao">
<ref bean="UserDao" />
</property>
</bean>
<bean id="UserAction" class="action.UserAction" > 

<property name="userservice">
<ref bean="userservice" />
</property>
</bean>
</beans>

异常:
java.lang.NullPointerException
action.UserAction.login(UserAction.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:364)
com.opensymphony.xwork.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:216)
com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:190)
com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:168)


谢谢各位慷慨指点,这个难题已经困扰我很久了
#64楼   2011-12-12 09:20 |  yovii   
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns=" http://java.sun.com/xml/ns/j2ee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>webwork</filter-name>
<filter-class>
com.opensymphony.webwork.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>webwork</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-*.xml,classpath:applicationContext*.xml
</param-value>
</context-param>
<taglib>
<taglib-uri>/webwork</taglib-uri>
<taglib-location>
/WEB-INF/lib/webwork-2.2.4.jar
</taglib-location>
</taglib>

</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值