Search code examples
htmlcssspringthymeleaf

Path Variables and ThymeLeaf no CSS


So I have the following controller that maps to the same ThymeLeaf template:

@GetMapping(value="/nextStep/{id}")
public String nextStep(@PathVariable int id) {
    return "nextStep";
}


@GetMapping(value="/xxx")
public String nextStep() {
    return "nextStep";
}

If I navigate to /xxx, the page has my css applied. If I navigate to /nextStep/10 the template displays but there is no css applied.

The template is simple:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <link rel="stylesheet" type="text/css" href="my.css">
        <link rel="icon" type="image/png" href="favicon.png">
    </head>
    <body>
Hello world<br>
    </body>
</html>

There are no exceptions thrown in this example.


Solution

  • you need put CSS path like this

     <link rel="stylesheet" type="text/css" th:href="@{/css/my.css}"/>