8

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.

1 Answer 1

11

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.