Brian
Brian

Reputation: 616

Spring Boot link to static content

I have a spring boot app with a static and templates folder within the src/main/resources folder. I am trying to link to a static page like:

<link href="../layout/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="../layout/index.css" rel="stylesheet">

I have tried about everything I can think of to get it to work. If i change it to

<link href="http://localhost:8080/layout/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="http://localhost:8080/layout/index.css" rel="stylesheet">

it works, but then it shows in my page source, and as always I want to hide my implementation from my users. What is a way I can access this static content without hard coding my domain, and without using localhost?

Thanks, Brian

Upvotes: 0

Views: 1277

Answers (1)

Patrick
Patrick

Reputation: 12734

Spring-boot looks up the static resouce by default under src/main/resources/static/

So if you dont have any folder structure after static/ like static/index.css this should work:

<link href="/index.css" rel="stylesheet">

But if you have a structure folder after static/ like static/dist/css/bootstrap.min.css this should work:

<link href="/layout/dist/css/bootstrap.min.css" rel="stylesheet">

So you just have to add the url after static in your link.

P.S.:

If you use thymeleaf as template engine you should also add the th:href tag to your link.

<link href="/layout/dist/css/bootstrap.min.css" rel="stylesheet" th:href="@/layout/dist/css/bootstrap.min.css">

Upvotes: 1

Related Questions