3

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 />
Community
  • 1
  • 1
Jack
  • 6,430
  • 27
  • 80
  • 151
  • Put it in `webapp` Do you have Spring Security setup? – Neil McGuigan Aug 31 '15 at 03:02
  • @NeilMcGuigan No I do not have that setup. – Jack Sep 01 '15 at 00:29
  • @NeilMcGuigan If I put that in webapp how to give access to that? do I need to configure anything in my Spring application or Tomcat? I have a test.jsp file in my root folder which I can access but not this robots.txt file. – Jack Sep 01 '15 at 02:53

1 Answers1

3

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

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • would robots.txt be available on www.domain.com/robots.txt? does that mean you are mapping /resources/** to two different locations? / and /Meta-INF/.... ? – Jack Sep 01 '15 at 09:45
  • 1
    Hi, how to configure the same using Java configs instead of xml configs? Thanks. – Battle_Slug May 03 '17 at 03:10