Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring mvc website on root ("/")

I want to map spring mvc controller to root (/**) path (and not sub-folder such as "/something") while making exceptions with mvc:resources (open to another method).

This should be the ABC of that framework but apparently is a very complicated stuff to ask of it.

My app-servlet.xml has these obvious mapping exceptions:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" />

And I have this controller:

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

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

@Controller
@RequestMapping("/**")
public class MainController {

    @RequestMapping(method = RequestMethod.GET)
    public String service(final HttpServletRequest request) {
        final String servlet_path = request.getServletPath();
        System.out.println(String.format("%s %s", new Date().toString(), servlet_path));
        return "test";
    }
}

Now when I hit "/" or "/test" or "/test/page" I get output like:

Fri Aug 03 00:22:12 IDT 2012 /
Fri Aug 03 00:22:13 IDT 2012 /favicon.ico

.. see? service() is being called for /favicon.ico even when it's explicitly excluded!

Now I guess there's some "priority" to the @Controller over the XML, still, how do I make the exclusion work?

A simple requirement - have the website on "/".

P.S This answer answers to a very similar question.

Another note: This question is not about tomcat context.

like image 781
Poni Avatar asked Aug 02 '12 21:08

Poni


1 Answers

I'd like to clarify that instead of rewriting the <mvc:annotation-driven/> one can change the declaration order of the handler directives:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" order="0"/>
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0"/>
<mvc:annotation-driven/>
like image 50
Mr_Mig Avatar answered Sep 22 '22 08:09

Mr_Mig