`
devsky
  • 浏览: 25352 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

在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了。

 

 

 

分享到:
评论

相关推荐

    struts.xml和applicationContext.xml、web.xml的配置

    struts.xml和applicationContext.xml、web.xml的配置

    struts、applicationContext配置文件移动后web.xml配置示例

    使用myeclipse8.5搭建SSH后,将struts.xml和applicationContext.xml移动到别的地方,示例中为webroot下的config文件夹中,web.xml中需要做的修改示例。其中对于返回上一层方式不同的myeclipse可能不同,如有的用../...

    Struts2.0_Hibernate3_spring2.0 实现用户注册

    Struts2.0_Hibernate3_spring2.0 部署Web应用请按如下步骤进行: 1. 进入reg_login路径下,将mysql.sql脚本中的语句导入MySQL数据库。 2. 修改reg_login\WEB-INF路径下的applicationContext.xml文件,将其中的...

    AutoCode代码生成器(SSH版)

    【用于JAVA开发中的 Struts2.0 + Spring2.0 + Hibernate3.2 框架整合程序开发】 强大的支撑功能,堪称JAVA SSH编程的利器,你绝对值得拥有! 自动生成以下所有内容(即:完整的Java SSH支持的工程) Sturts2.0配置--&gt;...

    SSH代码生成工具 SSH代码生成器

    Sturts2.0配置--&gt; sturts.xml、struts.properties Spring2.0配置--&gt; applicationContext.xml Hibernate3.2配置--&gt; hibernate.cfg.xml JSP文件--&gt; 具有Struts2.0支持的增、删、改、查页面及自定义查询、自动分页等...

    ssh代码生成器轻松、快捷

    用于JAVA开发中的 Struts2.0 + Spring2.0 + Hibernate3.2 框架整合程序开发】 强大的支撑功能,堪称JAVA SSH编程的利器,你绝对值得拥有! 自动生成以下所有内容(即:完整的Java SSH支持的工程) Sturts2.0配置--&gt; ...

    AutoCode代码生成器【SSH版】

    【用于JAVA开发中的 Struts2.0 + Spring2.0 + Hibernate3.2 框架整合程序开发】 强大的支撑功能,堪称JAVA SSH编程的利器,你绝对值得拥有! 自动生成以下所有内容(即:完整的Java SSH支持的工程) Sturts2.0配置--&gt;...

    整合struts2和spring源代码(可以直接在tomcat中运行)

    注意:struts2-spring-plugin-2.3.8.jar和commons-logging.jar文件可以在 struts框架中的找到而spring.jar 在spring框架中 2. 配置struts.objectFactory属性值: 在struts.xml文件中添加 &lt;constant name="...

    spring考试通过必备材料.docx

    在struts.xml中配置页面跳转关系, 21 在applicationContext.xml中配置类之间的调用关系, 22 通知 23 通知文件MyAdvice,schema方法的通知 23 在applicationContext.xml中配置通知--通知文件logAdvice 25 引用AOP的...

    struts2+spring+hibernate整合示例

    b 编写实体类,加入hibernate注解,编写方法类测试类,在applicationContext.xml中添加hibernate模板类配置以及包扫描语句 。在类中添加spring bean注解。 c 测试类中 主动解析applicationContext.xml ,获取bean ...

    applicationContext.xml

    spring struts hibernate 的配置文件

    ssh框架的XML文件

    SSH框架所需的XML文件,struts.xml+applicationContext.xml+hibernate.cfg.xml

    Extjs3.2+struts2.0+spring2.5+hibernate3.5+weblogic10+oracle10g含全包5

    proxool_cofig.xml为连接池配置 此项目可做基础项目开发原型方便,启动此项目在weblogic10中会有antlr-2.7.6rc1.jar此包的异常请配置其先加载并将次包考入 bea\wlserver_10.0\server\lib目录下,再将bea\user_projects...

    struts2驱动包

    java-struts2.2的驱动包 2009-8-29 14:02:04 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production ...

    Struts2.3.16.1+Hibernate3.6.10+Spring3.2.8整合

    Struts2.3.16.1+Hibernate3.6.10+Spring3.2.8整合 能够运行,没有任何问题 另外附带applicationContext.xml、struts.xml、hibernate.cfg.xml

    Spring 2.0 开发参考手册

    2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. JDBC 2.5. Web层 2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. ...

    ssh实现分页功能,一个简单的小项目

    主要是三个配置文件 web.xml struts-config.xml applicationContext.xml 数据库是sql2000

    Spring4整合Struts2 jar包

    包含Spring4 jar包和Struts2 jar包和整合之后的完整jar包。还有web.xml,struts.xml和applicationContext.xml文件,导入工程后可直接进行基于Spring4和Struts2上的开发。

Global site tag (gtag.js) - Google Analytics