Announcement Announcement Module
Collapse
No announcement yet.
dispatcher xml config is confusing me right now. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 10:34 AM.

  • #2

    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.

    Comment

    Working...
    X