-1

我有 application-context.xml 文件和一个名为 UserController2-servlet.xml 的子 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

<?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>

我如何从 application-context.xml 引用 UserController2.xml 即我需要在这两个 xml 之间创建一个链接(我不想将它们结合起来)。有人可以告诉我它是如何实现的吗?

4

2 回答 2

0

ArunM 给出的答案是正确的,尽管我认为您的意思是如何在 Web 应用程序中注册应用程序上下文和 servlet 上下文?

如果这是您的意思,您需要将它们添加到 web.xml 文件中。

首先像这样将 application-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>
于 2013-04-13T13:48:36.023 回答
0

您可以使用如下所示的导入标签将 application-context.xml 导入 UserController2-servlet.xml。将以下条目添加到 UserController2-servlet.xml

 <import resource="application-context.xml"/>
于 2013-04-08T06:21:29.443 回答