Cyril Deba
Cyril Deba

Reputation: 1210

Servlet Mapping Issue

I have a spring-based web application. In a controller I specified the following:

@RequestMapping(value = "/foo/index.html", method = RequestMethod.GET)
public ModelAndView handleIndex(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    return new ModelAndView("public/foo/index");
}

The application web.xml servlet mapping:

<servlet-mapping>
    <servlet-name>jib</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

When I make a request to the http://myhost/foo/index.hml everything works fine, but when I am trying to invoke http://myhost/foo/ - I receive a 404 error.

My question is how I can handle http://myhost/foo/ request exactly how I handle the http://myhost/foo/index.html request?

Upvotes: 0

Views: 133

Answers (2)

Cyril Deba
Cyril Deba

Reputation: 1210

Ok, the solution is shown below

  1. Change the servlet-mapping section in the web.xml file

    <servlet-mapping>
    <servlet-name>jib</servlet-name>
    <url-pattern>/</url-pattern>
    

  2. Change the @RequestMapping value parameter:

    @RequestMapping(value = {"/foo/", "/foo/index.html"}, method = RequestMethod.GET)
    public ModelAndView handleIndex(HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    return new ModelAndView("public/foo/index");
    }
    

Now, the handleIndex method handles all requests for /foo/ and /foo/index.html paths

Hope it will help somebody

Upvotes: 0

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40061

You are not mapping anything to /foo, only /foo/index.html. You could probably teach your tomcat/jboss/whatever to redirect requests to index.html for directories but it won't happen automatically.

As @BalusC points out, adding index.html to might do it (although I'm pretty sure that is the default of tomcat already). It's worth a try.

Upvotes: 1

Related Questions