`
王者之剑
  • 浏览: 194416 次
  • 性别: Icon_minigender_1
  • 来自: 湖北
社区版块
存档分类
最新评论

Struts2+Spring2.5+Hibernate3.2实例教程(2-2)V0.1

    博客分类:
  • java
阅读更多

2.集成Spring和Hibernate
1)  Struts2和Spring2集成
1.1)将D:\JavaTools\struts-2.1.2\lib\下的
struts2-spring-plugin-2.1.2.jar复制到
D:\Project\Bitrac\WebRoot\WEB-INF\lib\
1.2)在web.xml文件中加入Spring配置:
<!--默认加载/WEB-INF 目录下的applicationContext.xml -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加入后完整的文件内容如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < web-app   id = " bitrac " 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 " >
  3.     < display-name > Bitrac Blog </ display-name >
  4.    
  5.     < filter >
  6.         < filter-name > Struts2 </ filter-name >
  7.         < filter-class >
  8.             org.apache.struts2.dispatcher.FilterDispatcher
  9.         </ filter-class >
  10.     </ filter >
  11.  
  12.     < filter-mapping >
  13.         < filter-name > Struts2 </ filter-name >
  14.         < url-pattern > /* </ url-pattern >
  15.     </ filter-mapping >
  16.    
  17.     <!--默认加载/WEB-INF 目录下的applicationContext.xml -->
  18.     < listener >
  19.         < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
  20.     </ listener >
  21.    
  22.     < welcome-file-list >
  23.         < welcome-file > index.jsp </ welcome-file >
  24.     </ welcome-file-list >
  25.  
  26. </ web-app >


    1.3)新建Struts配置文件Bitrac\src\default.xml,内容如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < ! DOCTYPE   struts PUBLIC
  3.     " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
  4.     " http://struts.apache.org/dtds/struts-2.0.dtd " >
  5.    
  6. < struts >
  7.     < package   name = " default " extends = " struts-default " >
  8.         < action   name = " admincp " >
  9.             < result > /admin/login.jsp </ result >
  10.         </ action >      
  11.     </ package >
  12. </ struts >


这个配置文件将用来存放前台的action配置
    1.4)新建Struts配置文件Bitrac\src\admin.xml,内容如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < ! DOCTYPE   struts PUBLIC
  3.     " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
  4.     " http://struts.apache.org/dtds/struts-2.0.dtd " >
  5.    
  6. < struts >
  7.     < package   name = " admin " extends = " struts-default " namespace = " /admin " >
  8.         < action   name = " login " class = " adminAction " method = " login " >
  9.             < result   name = " success " > /admin/index.jsp </ result >
  10.             < result   name = " input " > /admin/login.jsp </ result >
  11.         </ action >      
  12.     </ package >
  13. </ struts >


这个文件将用来存放后台管理界面的Action配置
    1.5)修改Bitrac\src\struts.xml,内容如下:

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < ! DOCTYPE   struts PUBLIC
  3.     " -//Apache Software Foundation//DTD Struts Configuration 2.0//EN "
  4.     " http://struts.apache.org/dtds/struts-2.0.dtd " >
  5.    
  6. < struts >
  7.     < constant   name = " struts.devMode " value = " true " />
  8.     < constant   name = " struts.configuration.xml.reload " value = " true " />
  9.     < constant   name = " struts.action.extension " value = " jspx " />
  10.    
  11.     < include   file = " default.xml " />
  12.     < include   file = " admin.xml " />
  13. </ struts >


这里配置了一些全局常量,struts.action.extentsion是访问action时的后缀,通常用的有action或者do,我的习惯是用jspx,例如/admin/login.jspx,将调用admin.xml里定义的login action。
   1.6)新建Spring配置文件

  1. <? xml version = " 1.0 " encoding = " UTF-8 " ?>
  2. < beans   xmlns = " http://www.springframework.org/schema/beans "
  3.     xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance "
  4.     xmlns:aop = " http://www.springframework.org/schema/aop "
  5.     xmlns:tx = " http://www.springframework.org/schema/tx "
  6.     xsi:schemaLocation = "
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  9.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd " >
  10.  
  11.     < bean   name = " adminAction " class = " com.albertsong.bitrac.action.AdminAction " />
  12. </ beans >


注 意这里的bean name是adminAction,再看Bitrac\src\admin.xml里的class="adminAction",Spring和 Struts的结合点就在这里,后台处理这一结合的是struts2-spring-plugin-2.1.2.jar。如果不使用spring,在 admin.xml中将是class="com.albertsong.bitrac.action.AdminAction"。

 

14
2
分享到:
评论
7 楼 frenzy917 2008-11-21  
新建Spring配置文件
名字是applicationContext.xml?
6 楼 ruvuoai 2008-09-05  
学习中,加油,跟踪你

5 楼 zgxzowen 2008-09-03  
不知所云。。。。。。。。
4 楼 ydcsh 2008-09-02  
感觉在说天书,目的是什么?部署你自己的SSH?还是规范的SSH?
3 楼 王者之剑 2008-09-01  
@Jason(aijun)
加油,坚持
2 楼 王者之剑 2008-09-01  
@312947780@qq.com
什么意思?
1 楼 Jason(aijun) 2008-09-01  
加油

相关推荐

Global site tag (gtag.js) - Google Analytics