1

I have created sample Spring MVC REST Maven project with following folder structure Folder structure

ResourceHandlerRegistry configuration as follows

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.raju.spring_app")
public class RootConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static_res/*").addResourceLocations("/WEB-INF/html/static_res/");
    }
//Other methods 
}

Servlet mapping as follows

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/", "/static_res/*" };
    }
    //Other Methods
}

The problem is whenever I tried to access resource http://localhost:8080/spring4_rest_angular_demo/static/css/app.css

I got 404 error. I want to keep this folder structure to get css IntelliSense suggestions in index.jsp file.

<link href="static_res/css/app.css" rel="stylesheet"></link>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rajendra Thorat
  • 3,248
  • 3
  • 15
  • 24

2 Answers2

1

Few corrections :

Replace

return new String[] { "/", "/static_res/*" };

with

return new String[] { "/" };

and

registry.addResourceHandler("/static_res/*")

with

registry.addResourceHandler("/static_res/**")

Also, the right path is

http://localhost:8080/spring4_rest_angular_demo/static_res/css/app.css

and not

http://localhost:8080/spring4_rest_angular_demo/static/css/app.css

Bnrdo
  • 5,325
  • 3
  • 35
  • 63
0

With Spring 3.0.4.RELEASE and higher you can use

<mvc:resources mapping="/resources/**" location="/public-resources/"/>

As seen in http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-static-resources

Also, you should avoid putting pages in WEB-INF. Put the folder with html/css/js higher in hierarchy, under the web app folder. Generally, in WEB-INF there should be only configuration xml files.

m.aibin
  • 3,528
  • 4
  • 28
  • 47
  • 1
    1) He is using Java config equivalent of the code you posted. 2) You should put resources outside WEB-INF but the JSPs should remain inside WEB-INF folder. This way it won't be directly accessible by the client - it should be served by a controller. – Bnrdo Dec 08 '15 at 08:16