2013-04-08 66 views
-1

我有應用程序context.xml文件並命名爲UserController2-servlet.xml.The文件如下如何鏈接兩個XML文件

UserController2-servlet.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" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 


<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="index.htm">indexController</prop> 
     </props> 
    </property> 
</bean> 

<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="myDataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>Spring.project2.User</value> 
      </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 
</bean> 

<bean id="myUserDAO" class="project2.UserDAOImpl"> 
    <property name="sessionFactory" ref="mySessionFactory"/> 
</bean> 

<bean name="/user/*.htm" class="project2.UserController2" > 
    <property name="userDAO" ref="myUserDAO" /> 
</bean> 

<bean name="indexController" 
class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
     p:viewName="userForm" /> 

</beans> 
子xml文件

應用程序的context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org 
/dtd/spring-beans.dtd"> 
<beans> 
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/fde" /> 
    <property name="username" value="dgdg" /> 
    <property name="password" value="dgd" /> 

</bean> 
    </beans> 

如何從應用程序的context.xml也就是指的UserController2.xml我需要創建這兩個XML之間的聯繫(我不想將它們組合)。可一些請告訴我怎麼可以實現?

回答

0

您可以使用import標籤將application-context.xml導入到UserController2-servlet.xml中,如下所示。添加下面進入UserController2-servlet.xml中

<import resource="application-context.xml"/> 
0

答案是ArunM放棄是正確的,雖然我總覺得你的意思是你怎麼註冊的應用程序上下文和Web應用程序的servlet上下文?

如果這是你的意思,你需要將它們添加到web.xml文件中。

首先添加的應用程序的context.xml到web.xml這樣

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <!-- add you application context location --> 
    <param-value>classpath*:META-INF/spring/application-Context.xml</param-value> 
</context-param> 

然後在web.xml中註冊servlet配置

<!-- Handles Spring requests --> 
<servlet> 
    <servlet-name>someName</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/spring/UserController2-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>someName</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping>