Results 1 to 2 of 2

Thread: dispatcher xml config is confusing me right now.

  1. #1
    Join Date
    May 2011
    Posts
    3

    Default dispatcher xml config is confusing me right now.

    I got a REALLY simple project:
    Code:
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>3.1.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>3.1.1.RELEASE</version>
            </dependency>
    dispatcher xml:
    Code:
        <context:component-scan base-package="be.wim.controller" />
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    applicationcontext xml:
    Code:
       <context:component-scan base-package="be.wim.service" />
    With this web.xml:
    Code:
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/spring-config.xml</param-value>
        </context-param>
                   
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>     
        
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/mvc-config.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    Nothing special. Everything works.
    Now - since the dispatcher context is a child of the main application context - why can't I move the component scan for the controllers to there? If it's not in the dispatcher servlet config, the dispatcherservlet does not know any controller...


    And also: How come this works without <context:annotation-config /> or <mvc:annotation-driven /> ?
    I made the controller with @Controller, and @RequestMapping ....
    Last edited by wim DC; Apr 26th, 2012 at 09:34 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default


    Now - since the dispatcher context is a child of the main application context - why can't I move the component scan for the controllers to there? If it's not in the dispatcher servlet config, the dispatcherservlet does not know any controller...
    Because the infrastructure classes used by the dispatcherServlet (HandlerMappings and HandlerAdapter) by default only look in the current context not in parent contexts.

    And also: How come this works without <context:annotation-config />
    This is implied by the component-scan

    or <mvc:annotation-driven /> ?
    Due to the default settings of the dispatcher servlet.

    I suggest a read of the reference guide as all this is explained in there in more detail.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •