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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With