1
votes

I am following this Spring Docs and configured web.xml and applicatioinContext.xml to use feature Single root context in Spring Web MVC i.e. there is no Servlet WebApplicationContext; all request will be served by Root WebApplicatioinContext.

But problem is HTTP requests are not being delegated to controllers of root WebApplicationContext.

web.xml:

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/root-context.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

root-context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="PostController" class="org.my.controllers.PostController">
    </bean>

</beans>

Controller:

@Controller
public class PostController {
    @RequestMapping(value= "/controller", method = RequestMethod.GET)
    @ResponseBody
    public String foo() {
        return "Response!";
    }
}

So When I hit URL http://localhost:8080/PracticeJavaWeb/controller in browser it is showing 404

Server log saying:

WARNING: No mapping found for HTTP request with URI [/PracticeJavaWeb/controller] in DispatcherServlet with name 'dispatcher'

Spring Docs saying:

It is also possible to have just one root context for single DispatcherServlet scenarios.

2

2 Answers

3
votes

Just one small change to the root-context.xml file, add this:

<mvc:annotation-driven></mvc:annotation-driven>

mvc:annotation-driven enables context path to recognize spring mvc annotation in order to dispatch the request to the controllers.

Also here is a full example of a root-context.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <mvc:annotation-driven></mvc:annotation-driven>

    <bean id="welcomeService" class="com.myorg.controller.MyService"></bean>
    <bean id="welcomControler" class="com.myorg.controller.WelcomeController"></bean>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
1
votes

You need to add <mvc:annotation-driven /> tag in root-context.xml.

<mvc:annotation-driven /> tag ensures your request are dispatched to the controllers.

And don't forget to add mvc namespace as well in root-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>