2

Using Spring MVC, I want to map the root path of my Java web application to a controller.

Controller class :

package project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {

    @RequestMapping(value={"/", "/index"})
    public String home() {
        return "index";
    }
}

The home() method is mapped on “/” and “/index” url fragments. /index works fine:

http://localhost:8080/JavaSpringWebProject/index

But “/” generates an http 404 error.

http://localhost:8080/JavaSpringWebProject/

I used the debugger to see if the doService() method in the DispatcherServlet is called as it should be. It is called with /index but not with /. My guess is that Tomcat (v7) does not call the DispatcherServlet. But why?

I’ve created a test project (Dynamic Web project from Eclipse WTP) with Spring 4.0.6 to isolate the problem. It can be checked out from svn at http://code.google.com/p/basic-spring-java/

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Project</display-name>

      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>

      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
      </context-param>
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
      </servlet-mapping>

    </web-app>

Why is my controller not activated with the “/” path? Many thanks!

Ramzi G.
  • 59
  • 1
  • 10

1 Answers1

2

EDIT : first suggestion was wrong or at least not enough


I do not know why, but in my controllers, I had to use "" to have access to root Urls.

Simply use @RequestMapping(value={"", "/", "/index"}) and it should work.


Now the correct way :

For reasons only known to Spring developpers (and maybe some other experts), mapping spring dispacher servlet to "/" breaks spring root url detection. I only find an element on that in last answer of that other post : In a servlet mapping in Spring MVC how do I map the root of a url pattern directory?, but I tested it with Spring 3.2.4 and it seems to be true.

So in web.xml, you must write :

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

Unfortunately, this will catch everything, and it is no use trying to have default server serving anything. You will have to configure spring internal resource management through

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

if using xml config or

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/public-resources/");
  }
}

if using java config.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    Thanks for the suggestion. I just tried, but it does not work for me. – Ramzi G. Aug 01 '14 at 07:42
  • 1
    This answer cannot work in this situation since the DisptacherServlet is not even called. The problem is more to activate the dispatcherServlet (SpringMVC) for "/" than anything internal in SpringMVC. – John Rizzo Aug 05 '14 at 07:48