Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf + CSS+SpringBoot

I have a problem with CSS and Thymeleaf.

In my Spring boot app, I have this structure:

  • src/main/resource/static/css (for css files)
  • src/main/resource/static/templates (for html file)

Now, with my html page named ErrorPage and css file named Layout.css, using Thymeleaf I have, in the head of ErrorPage:

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

But this does not work.

What am I doing wrong?

like image 601
Luca Sepe Avatar asked Jan 11 '17 09:01

Luca Sepe


3 Answers

Move your template folder right under resources:

  • src/main/resource/static/css (for CSS files);
  • src/main/resource/templates (for HTML templates).

Then correct the link tag as follows:

<link href="../static/css/Layout.css" th:href="@{/css/Layout.css}" rel="stylesheet" />
like image 76
DimaSan Avatar answered Oct 22 '22 12:10

DimaSan


Move your template folder right under resources:

src/main/resources/static/css (for CSS files);
src/main/resources/templates (for HTML templates).

Then correct the link tag as follows (relative or absolute):

<link href="../css/firstcss.css" rel="stylesheet">
<link href="/css/secondcss.css" rel="stylesheet">

The old solution with static in front doesn't work for me.

like image 4
Adrian Avatar answered Oct 22 '22 11:10

Adrian


The main culprit of this behaviour is a custom security configuration which is very likely you are doing in your WebSecurityConfigurerAdapter subclass. If you use SpringBoot 2+ version you should add the following line in your WebSecurityConfigurerAdapter configuration

.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()

like image 3
Semyon Evgrafov Avatar answered Oct 22 '22 11:10

Semyon Evgrafov