struts2+spring3+hibernate3整合(一)(转载)

1.首先配置struts2.1.8

1.1 这东西大家熟烂了.我个手工添加的.jar包如下:

struts2+spring3+hibernate3整合(一)(转载)

对于struts2.1.8,我一般最开始是用最少量的包,如果要添加json等一些,我会另外再到官网找最新的.
1.2 修改web.xml
view plaincopy to clipboardprint?
<!-- struts2.1.6  FilterDispatcher START --> 
    <filter> 
        <filter-name>struts2</filter-name> 
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter> 
      
    <filter-mapping> 
        <filter-name>struts2</filter-name> 
        <url-pattern>/*</url-pattern> 
        <dispatcher>FORWARD</dispatcher> 
        <dispatcher>REQUEST</dispatcher> 
    </filter-mapping> 
<!-- struts2.1.6  FilterDispatcher END --> 
<!-- struts2.1.6  FilterDispatcher START -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
<!-- struts2.1.6  FilterDispatcher END -->
加入这一段就好了.很方便的struts2配置.


1.3 添加struts.xml


在src下新建立struts.xml.内容如下
view plaincopy to clipboardprint?
<constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
    <constant name="struts.devMode" value="false" /> 
    <include file ="struts-default.xml" />   
      
    <package name="default" namespace="/" extends="struts-default"> 
        <default-action-ref name="test" /> 
        <action name="test"  class="testActionBean" >    
            <result>/test.jsp</result>    
        </action> 
          
    </package> 
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <include file ="struts-default.xml" />
   
    <package name="default" namespace="/" extends="struts-default">
     <default-action-ref name="test" />
     <action name="test"  class="testActionBean" > 
            <result>/test.jsp</result> 
        </action>
     
    </package>
也非常简单..大家能从这里看到.我新添加了一个test.jsp作为测试用页面.还有建立了包test.action;里面有个action:TestAction;


至于里面做问么,大家喜欢怎么测试就怎么测试吧.

2. 配置spring3.0.2 结合 struts2.1.8
2.1 添加spring3.0.2的jar包如下:


 struts2+spring3+hibernate3整合(一)(转载)

其中蓝色的包,代表是加进去了项目的lib里面了.很多吧?是的,因为我懒,这些全加进去不会有冲突,以后还可能得用上不用找.所以就全加了几乎.
这个是我写这篇文章时最新的spring3.0.2的release包.你们看的时候可能不同咯.


2.2 修改web.xml


view plaincopy to clipboardprint?
<!-- 配置spring3.x的上下文配置文件.没有的话spring会报错 --> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath*:applicationContext*.xml</param-value> 
    </context-param> 
      
    <!-- struts2.1.6  FilterDispatcher START --> 
    <filter> 
        <filter-name>struts2</filter-name> 
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
    </filter> 
      
    <filter-mapping> 
        <filter-name>struts2</filter-name> 
        <url-pattern>/*</url-pattern> 
        <dispatcher>FORWARD</dispatcher> 
        <dispatcher>REQUEST</dispatcher> 
    </filter-mapping> 
    <!-- struts2.1.6  FilterDispatcher END --> 
      
    <!-- spring3.x上下文监听器 START --> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <!-- spring3.x上下文监听器 END --> 
<!-- 配置spring3.x的上下文配置文件.没有的话spring会报错 -->
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:applicationContext*.xml</param-value>
 </context-param>
 
 <!-- struts2.1.6  FilterDispatcher START -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
 
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    <!-- struts2.1.6  FilterDispatcher END -->
   
    <!-- spring3.x上下文监听器 START -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    <!-- spring3.x上下文监听器 END -->
这里跟配置struts2.1.8的时候是有相同的地方,只是加了2段代码.一段是spring3的上下文监听器,一段是spring3的上下文配置文件.不难发现.把struts2.1.8的配置夹在中间了.


2.3 添加applicationContext.xml,内容如下:


view plaincopy to clipboardprint?
<!-- struts2的TestAction托管给spring3.在spring的配置文件也有对应的配置bean --> 
    <bean name="testActionBean" class="test.action.TestAction" /> 
<!-- struts2的TestAction托管给spring3.在spring的配置文件也有对应的配置bean -->
 <bean name="testActionBean" class="test.action.TestAction" />


这是其中最主要的内容.其他的applicationContext.xml格式你们可别节省了!这里我把struts2的action托管给spring3管理.(现实中我不喜欢,都只让spring管理数据访问层做事务.不过删掉就好了.)


2.4 修改struts.xml内容如下:


view plaincopy to clipboardprint?
<!-- Configuration for the default package. --> 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 
    <constant name="struts.devMode" value="false" /> 
    <include file ="struts-default.xml" />   
    <!-- 在struts.xml声明,action交予spring3.x托管 --> 
    <constant name="struts.objectFactory" value="spring"/> 
      
      
    <package name="default" namespace="/" extends="struts-default"> 
        <default-action-ref name="test" /> 
        <action name="test"  class="testActionBean" >    
            <result>/test.jsp</result>    
        </action> 
          
    </package> 
<!-- Configuration for the default package. -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <include file ="struts-default.xml" />
    <!-- 在struts.xml声明,action交予spring3.x托管 -->
    <constant name="struts.objectFactory" value="spring"/>
   
   
    <package name="default" namespace="/" extends="struts-default">
     <default-action-ref name="test" />
     <action name="test"  class="testActionBean" > 
            <result>/test.jsp</result> 
        </action>
     
    </package>


是的,不单只要在spring的配置文件做声明,在struts.xml里也要做声明,而且还有改变了哦.注意看action的地方.
对了,忘记说了,对于struts2-struts1-plugin-2.1.8.1.jar 要不要加呢?我是加了.但remove后,也没问题.迟点再研究吧.

posted on 2011-05-09 07:22  唐朝  阅读(528)  评论(0编辑  收藏  举报