[学习笔记]在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法

 在用SSH2做项目中,一般都是在一个struts.xml中配置控制转发,而用一个applicationContext.xml用来管理Bean。这对于一个简单的项目来说,就已经足够了。但是,对于一个大型项目的来说,团队成员很多,每个成员都在这两个配置文件里各写各的配置信息,这样会使这两个文件信息量过大,整合起来也不是很方便,阅读代码很困难。鉴于这种情况,是否有更好的解决办法呢?上网查了很久,也试了很久,终于有解决的办法了。下面就将我的配置信息罗列出来,供大家参考。

 

1.首先在src下建立一个总的struts.xml文件,该文件的作用是,加载每一个模块的配置文件信息(换句话说就是加载多个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>
    <include file="struts-default.xml" />

    <!--模块1 struts的XML文件-->
    <include file="config/struts-basicManager.xml" />
    
    <!--模块2 struts的XML文件-->
    <include file="config/struts-inspection.xml" />
    
    <!--模块3 struts的XML文件-->
    <include file="config/struts-curingManager.xml" />
     
    
</struts>

这样总的struts.xml文件就配置好了。

 

2.在src下新件一个包,包名为config。该包的作用是存放每一个分模块的struts的配置文件。例如我的一个配置文件是struts-curingManager.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>
    <package name="qdbridge" extends="struts-default" >
     <action name="ceshi" class="com.xtlh.qdbridge.action.Component_ReduceAction" method="list">
            <result name="success" >/MyJsp.jsp</result>
        </action>  
 </package>
</struts>

这个文件可以用来写多个action。

 

3.接下来就是配置总的applicationContext.xml文件了。
首先,为了管理方便,我在WEB-INF下新建一个conf文件夹,将WEB-INF下的applicationContext.xml文件拖到conf文件夹下,这样作的目的是方便管理。内容如下:

<?xml version="1.0" encoding="UTF-8" ?> 
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <import  resource="applicationContext-shenwen.xml"/>
    <!-- 配置数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <property name="driverClass">
      <value>oracle.jdbc.driver.OracleDriver</value>
      </property>
      <property name="jdbcUrl">
           <value>jdbc:oracle:thin:@192.168.0.47:1521:qdhw</value>
      </property>
      <property name="user">
          <value>qdhw</value>
      </property>
      <property name="password">
          <value>qdhw</value>
      </property>
      <property name="minPoolSize">
          <value>15</value>
      </property>
      <property name="acquireIncrement">
          <value>5</value>
      </property>
      <property name="maxPoolSize">
          <value>25</value> 
      </property>
 </bean>
    
    <!-- 配置SessionFactory -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <!-- 指定HIbernate映射文件的路径 -->
  <property name="mappingDirectoryLocations">
   <list>
    <value>classpath:com/xtlh/qdbridge/entity</value>
   </list>
  </property>
  <!-- 配置Hibernate的属性 -->
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.Oracle9Dialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>
 
 
   
</beans>

大家会注意到,这里我用到了c3p0这个数据源连接池,目的是建立多连接,提高效率。在数据源的上方会看到有这么一句话,<import  resource="applicationContext-shenwen.xml"/>,这句话的意思

就是在总的applicationContext.xml中引入各个子模块的applicationContext配置文件,它的作用相当于java中的导包。需要注意的是,<import  resource="applicationContext-shenwen.xml"/>这一

句话,要放在所有bean配置的最前面。

 

4.同样,在WEB-INF目录下的conf文件夹下,新建一个子模块applicationContext配置文件(可以建立多个),我的文件名是:applicationContext-shenwen.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
 <bean id="crDao" class="com.xtlh.qdbridge.springImpl.Component_ReduceDaoImpl" >
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
  </bean>
</beans>

大家会看到,这其实和总的applicationContext.xml一样,但是请大家注意黑体部分是不一样的,以前是<ref local="sessionFactory"/>现在变成了<ref bean="sessionFactory"/>。不然会出错的。这

里可以注入多个Dao接口。

 

5.启动Tomcat,会发现报错。这时不要急,就差一步了。是因为,刚才在第三步,把总的applicationContext.xml拖到了conf文件夹下,系统找不到,这时只要在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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <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>
  </filter-mapping>
  
  <context-param>  
  <param-name>contextConfigLocation</param-name>  
  <param-value>/WEB-INF/conf/applicationContext.xml</param-value>  
  </context-param>

  
  
  <!-- log4j配置 -->
  <context-param>   
    <param-name>log4jConfigLocation</param-name>   
    <param-value>/WEB-INF/log4j.properties</param-value>   
  </context-param>   
  
  <context-param>   
 <param-name>log4jRefreshInterval</param-name>   
 <param-value>60000</param-value>   
  </context-param>
  
  <listener>   
 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>   
 </listener>
   
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
</web-app>


这样就一切OK了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值