3

I am using springboot with thymeleaf to develop a website. The framework can load bootstrap css files cerrectly when not using uritemplating, but when I change to uritemplating then bootstrap css breaks.

This works perfectly

@GetMapping("/update_avatar")
public String update_avatar(){

    return "update_avatar";
}

This breaks css loading

@GetMapping("/update_avatar/{userid}")
public String update_avatar(@PathVariable String userid){

    System.out.println("Testing Variable: "+userid);
    return "update_avatar";
}

Here is how I include my stylesheet

<link rel="stylesheet" type="text/css"
      href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />

<link rel="stylesheet" th:href="@{/css/main.css}"/>
<link rel="stylesheet" th:href="@{/css/tutors.css}"/>

Please note that I included Bootstrap using maven by doing the following

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.7</version>
</dependency>

I tried to implement viewRsolver based on this solution but it did not work for me CSS not loading in Spring Boot

8
  • Can you post your template code that includes the <link... element and tell us where the css file lives in the project directory structure? Commented Jan 28, 2018 at 17:25
  • I just included the snippets above, I used maven to include bootstrap in the project Commented Jan 28, 2018 at 17:34
  • Got it, have you added the resource handler (#4) baeldung.com/maven-webjars ? Commented Jan 28, 2018 at 17:41
  • Let me take a look at the article. Commented Jan 28, 2018 at 17:46
  • 1
    Just occurred to me that you likely need to change your webjars to be absolute: /webjars not webjars. For the others, have a look at the rendered HTML and confirm that the paths are correct (e.g. they're rendering <link rel="stylesheet" href="/css/main.css"/>) Commented Jan 28, 2018 at 19:17

1 Answer 1

0

Oh, I finally got it to work even though I do not have a detailed explanation as to why the code did not work. I had this @EnableWebMvc annotation in my resource configurations class, and it was the one messing up everything. I had taken the annotation from the example on this site so if you know what it means, please do help with an explanation.

I also added registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/"); to my resource configurations class as Chris had suggested above.

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.