I cannot access Robots.txt in Spring-MVC

2019-01-20 14:57发布

问题:

I am trying to give access to robots.txt in Spring-MVC. To test the code, I put robots.txt in WebContent,Root and WEB-INF but I cannot access to any of them.

I've already applied answers of these questions 1,2,3 to no avail.

MyCode

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0" />
<mvc:annotation-driven />

回答1:

This works for me:

Put robots.txt directly under webapp

In mvc-dispatcher-servlet.xml have:

<mvc:default-servlet-handler/>

<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />

Use maven to build your war