在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法
来源:互联网 发布:大非农数据公布时间 编辑:程序博客网 时间:2024/06/08 15:18
在用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了。
转载:在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法
- 在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法
- 在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法
- [学习笔记]在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法
- 在Struts2 中 struts.xml的Action配置详解
- struts2-applicationContext.xml配置
- SSH框架整合时applicationContext.xml的配置,关于在main方法中测试注意的事项
- SSH中 applicationcontext.xml与web.xml的配置
- ssh整合!Struts.xml、hibernate与spring整合的applicationContext.xml、web.xml文件的配置!
- struts2.0中struts.xml配置…
- struts2.0中Struts.xml配置详解
- SSH框架中applicationContext.xml文件的配置
- applicationContext.xml 配置方法
- struts2中配置多个struts.xml配置文件
- 简单的struts2.0配置struts.xml
- struts2 的struts.xml配置
- struts2的struts.xml配置
- 在Spring的 applicationContext.xml文件中配置数据源
- Spring与Hibernate的整合applicationContext.xml中配置
- 安装ssh并配置免密码登录时遇到的问题
- EXCEL文档没有打开时,报错误"外部表不是预期的格式。"
- linux网络编程手记
- coursera 错题集
- 自定义ProgressDialog,实现超时功能
- 在struts2.0框架中,多struts.xml与多applicationContext.xml配置的方法
- Mac Pro 安装 双系统 window7
- DNS解析协议的C语言简单实现
- Lua游戏脚本热更新机制
- 分布式服务框架远程服务通讯介绍
- 日常:在MAC平台下,有关数据解析,HTML生成Excel档
- Unity3D: StartCoroutine 和 yield return 深入研究
- T001-UT001-0026--------文件读写练习——日志分析
- linux下 安装mysql数据库