0

I can not using ResourceHandler in my web app. Here is my ResourcesHandler impliment

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) 
{
    registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
    registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/");
    registry.addResourceHandler("/script/**").addResourceLocations("classpath:/script/");
}

My project structure :

project

My app in tomcat8 webapp foler :

enter image description here

I try this code to using javascript but it doesn't work :

<script src="${pageContext.request.contextPath}/script/angular.min.js"></script>

I try to remove "classpath:" in addResourceHandlers, no luck. How can I fix it?

Hai Hoang
  • 1,207
  • 2
  • 18
  • 35

2 Answers2

1

Problem solved! I forgot @EnableWebMvc annotation in my configuration class, many article about static resources in spring mvc doesn't mention this problem. I hope my problem will save other a little time if they meet this one.

Hai Hoang
  • 1,207
  • 2
  • 18
  • 35
-1

Use the following code:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) 
{
  registry.addResourceHandler("/css/**").addResourceLocations("../resources/css/");
  registry.addResourceHandler("/img/**").addResourceLocations("../resources/img/");
  registry.addResourceHandler("/script/**").addResourceLocations("../resources/script/");
}
Meyer
  • 1,662
  • 7
  • 21
  • 20
Shahriar Miraj
  • 307
  • 3
  • 14