Mahendran
Mahendran

Reputation: 2253

Spring-mvc component-scan and view-controller together is not working

The following is my application-context.xml

<context:component-scan base-package="org.godfather"></context:component-scan>
<mvc:view-controller path="/main.do" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
      <property name="prefix" value="/WEB-INF/" />
      <property name="suffix" value=".jsp" />
</bean>

Question: If I comment out <mvc:view-controller path="/main.do" /> then http://localhost:8181/tiles-app-1.1.1/header.do url is working, but if it is NOT commented out, then the url is not working, getting No mapping found for HTTP request with URI [/tiles-app-1.1.1/header.do] in DispatcherServlet with name 'mytiles' why is it so?

Web.xml

    <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/application-context.xml
    </param-value>
</context-param>  

<servlet>
    <servlet-name>mytiles</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>mytiles</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

Controller

package org.godfather;
@Controller
public class AppointmentController {

@RequestMapping("/header")
public void get() {
    System.out.println("get()");
}

Thanks in advance.

Upvotes: 2

Views: 1475

Answers (1)

ElderMael
ElderMael

Reputation: 7111

You are passing the same Spring Bean definition file (/WEB-INF/application-context.xml) to the ContextLoaderListener and to the DispatcherServlet.

The DispatcherServlet should have its own Spring Bean definition file

See this question. All Spring MVC controllers and specific MVC xml should be in a different file.

Upvotes: 1

Related Questions