3

I've been trying to get this working but no luck after several hours.

My structure is like this:

πŸ— src
└─── πŸ— main
    β”œβ”€β”€β”€ πŸ— java
    └─── πŸ— resources
        β”œβ”€β”€β”€ πŸ— META-INF
        β”‚   └─── πŸ— resources
        β”‚       └─── πŸ— bootstrap
        β”‚           β”œβ”€β”€β”€ πŸ— css
        β”‚           └─── πŸ— js
        β”œβ”€β”€β”€ πŸ— templates
        β”‚   └─── index.html
        └─── application.properties

Here is the WebConfig class:

@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
    }

}

If I use link to a web resource, it works:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>

If I'm trying to include a static resource, it doesn't work:

<link type="text/css" rel="stylesheet" href="../../../resources/bootstrap/css/bootstrap.min.css" 
    data-th-href="@{/resources/bootstrap/css/bootstrap.min.css}" />

Any help is highly appreciated.

Thank you

3 Answers 3

6

Avoid webapp folder with spring boot

Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.

Where to place static files then?

  • Spring Boot serves static files in these folders (relative to /src/main/resources/):

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources).

I suggest you reorganize your webapp files into /src/main/resources/static

Other answers

EDIT AFTER COMMENTS - Path to the static files

When accessing static files, the static folders (listed above) are the path's root.

Thus, to access your css file:

  • file in project: META-INF/resources/bootstrap/css/bootstrap.min.css
  • url: http://localhost:8080/bootstrap/css/bootstrap.min.css
  • link tag: <link type="text/css" rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
  • link tag: <link type="text/css" rel="stylesheet" data-th-href="@{bootstrap/css/bootstrap.min.css}" />
Sign up to request clarification or add additional context in comments.

10 Comments

Hi @Alex, thank you so much for your reply, really much appreciated. Sorry for late reply BTW but have been to work all day. I've changed the structure with META-INF/resources and there is where I've put all my assets, then from the index.html I wrote <link type="text/css" rel="stylesheet" href="/bootstrap/css/bootstrap.min.css" data-th-href="@{/bootstrap/css/bootstrap.min.css}" />. Apparently still doesn't work for some reason. I'm not sure what else to do though.
can you update your question and include your new struture ?
@Alex, the question has been updated :) Thank you so much
Did you also update your configuration, sush as "setPrefix"... ? Also, your link tag uses both href and data-th-href, isn't suppose to be either href or th-href ? Also, your href is not relative, try : <link type="text/css" rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css"/>
Hi @Alex, thank you for your reply. Sorry forgot to amend that on here, please see my latest update, I've removed the thymeleaf config class as it's no more necessary. I've seen online you can keep both: href and data th-href and should work. However, with both or only one still doesn't work. I'm not entirely sure where the problem lies. I've changed the href as you suggested but made no difference.
|
2

The issue has been solved. Unfortunately the problem wasn't from any of the above, it was from a controller mapped to "/". If you have the same problem I had, please make sure you don't have anything mapped to "/". Now all resources are accessible through the URL.

My new structure is as follow:

src
   main
       java
       resources
           static
               bootstrap
                  css
                  js
       templates
           index.html
       application.properties

Here is the WebConfig class:

@Configuration
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/");
    }

}

The link is being included in the HTML as:

<link type="text/css" rel="stylesheet" href="../bootstrap/css/bootstrap.min.css" 
    data-th-href="@{/bootstrap/css/bootstrap.min.css}" />

Comments

0

if you are facing such error after Spring Web MVC 5+

"The type WebMvcConfigurerAdapter is deprecated"

@Configuration
public WebConfig implements WebMvcConfigurer {
    // 
}

https://www.baeldung.com/web-mvc-configurer-adapter-deprecated

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.