I read so many posts about this issue but I just couldn’t solve it. I am trying to add a simple background image in my html page as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body {
background-image: url(../static/background.jpg);
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>
</body>
</html>
When I run the Spring application and went to localhost:8080/temp
, I gotta see my html page which I set in my controller as get request but all I got is just a white blank page. The thing is, if I manually right click my temp.html
file and open it in chrome, it works just fine, I can see my image. So I think there is a problem with spring boot or something?
Here’s my resources directory:
2
Answers
For those who struggle with this, if you are using Spring Security dependency, give permission to your files like this:
Then put your image under the static/img folder like this:
Put the CSS into your CSS file:
Lastly, add CSS link to your HTML file:
Hope this helps to someone!
Resources in
resources/static
are mapped to the root,/
. Templates are also mapped to the root,/
. That’s why the reference in the template should bebackground.jpg
.