2

How can I force static resources (HTML, CSS, JS) to be served under a different path than what is specified in the /webapp/ directory?

Say I have a resource at the following path in my webapp:

/src/main/webapp/client/mypage.html

I want clients to be able to access it from the following URL:

/myapp-context/v1/client/mypage.html

How can I specify a common root URL ("v1") for these resources in my web.xml is there a default filter or static resource servlet I can configure which will enable this behavior? Alternately, can I specify a base URL for the entire app beyond the context root?

RMorrisey
  • 7,637
  • 9
  • 53
  • 71

2 Answers2

3

Have you seen the class org.springframework.web.servlet.DispatcherServlet?

In the web.xml:

<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>

And in the dispatcher-servlet.xml:

<mvc:resources mapping="/v1/client/*.html" location="/client/pages/" />
<mvc:resources mapping="/v1/client/*.js" location="/client/js/" />
<mvc:resources mapping="/v1/client/*.css" location="/client/css/" />

If you want to use a base tag, see relative paths.

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
1

Define

<mvc:resources mapping="/v1/client/**" location="/client/" />

on dispatcher-servlet configuration.

Thanks!

roshan
  • 160
  • 1
  • 7