faz95
faz95

Reputation: 48

Java Spring with mustache keeps looking for files on server

I've been trying to develop a quick website using Java Spring with Mustache for templating but I have an issue when it comes to getting local files (css stylesheets or images). I'm running the website on intelliJ on http://localhost:8000 and for some reason it looks for every files on http://localhost:8000/Path/to/the/file and returns a 404 error on the inspector (no error in the console log)

Here are my file-tree and the code I'm using. The templates .mustache are in the templates directory.

filetree

<nav class="level">
    <div class="level-left">
        <div class="level-item">
            <img src="/img/flags/gb.svg"/>
        </div>
        <div class="level-item">
            <img src="/img/flags/fr.svg"/>
        </div>
        <div class="level-item">
            <img src="/img/flags/es.jpg"/>
        </div>
        <div class="level-item">
            <img src="/img/flags/ch.svg"/>
        </div>
        <div class="level-item">
            <img src="/img/flags/de.svg"/>
        </div>
    </div>
</nav>

I saw some posts telling to add a resource handler like this :

registry.addResourceHandler("/css/**").addResourceLocations("/resources/static/css");
registry.addResourceHandler("/img/**").addResourceLocations("/resources/static/img");
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/static/");

But no luck so far. Any help would be appreciated.

Upvotes: 0

Views: 1303

Answers (1)

Anna van den Akker
Anna van den Akker

Reputation: 708

Try to use this resource handler for your static resources

registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");

If it doesn't work you can check furher here - Spring Boot unabe to serve static image from resource folder

Upvotes: 1

Related Questions