Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 loading static resources

I got a spring MVC application with a bunch of css and js files currently placed in the src/main/java/resources/assets directory.

I read through the Spring Docs and some Tutorials on how to load these files for my templates using the ResourceHandlerRegistry class. I especially thought that the code snippets from this Tutorial would perfectly fit my project structure.

But I get always a 404 on my resource files.

Here is the Application/Configuration class I'm currently running with:

@Configuration
@EnableAutoConfiguration
@ImportResource("/applicationContext.xml") // only used for jpa/hibernate
@EnableWebMvc
@ComponentScan(basePackages = "at.sustain.docutools.viewer.presentation")
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String args[]) {
        SpringApplication.run(Application.class);
    }

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

    }

}

And here a HEADer used in my HTML files (placed in resources/templates):

<head>
    <!-- local Stylesheet -->
    <link href="css/style.css" rel="stylesheet" />
    <!-- local Javascript files -->
    <script src="js/general.js"></script>
    <script src="js/xmlhttp.js"></script>
    <!-- local Javascript libraries -->
    <script src="js/lib/filter.js"></script>
    <script src="js/lib/jquery.fs.zoomer.js"></script>
    <script src="js/lib/jquery.validate.js"></script>
</head>

The html file is loaded correctly through my controller classes but when trying to hit e.g. my style.css file (http://localhost:8080/css/style.css) I get an 404 as already mentioned.

I can't seem to find any more resources who could provide me with more information on this subject for Spring 4. Do I miss some configuration files? Or aren't the resource handler registrations fitting my structure? I'm looking forward to your responses.

like image 220
SakeSushiBig Avatar asked Jun 06 '14 10:06

SakeSushiBig


People also ask

How do spring boots use static resources?

Using Spring BootSpring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

How do I restrict static resources in Spring Security?

The Ant matchers match against the request path and not the path of the resource on the filesystem.So ignore any request that starts with "/resources/". This is similar to configuring http@security=none when using the XML namespace configuration.


1 Answers

You say that your stylesheets and JavaScript files are under "/assets". I'm going to assume you have directories "/assets/css" and "/assets/js". Then, given the following resource handler definition:

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

You would load these resources in your HTML like so:

<link href="/assets/css/style.css" rel="stylesheet" />
<script src="/assets/js/general.js"></script>
like image 199
James Sumners Avatar answered Sep 22 '22 17:09

James Sumners