Reputation: 390
My folder structure
ProjectName
src/main/java
src/main/resources
static
css
styles.css
main.css
js
...
templates
All my html files are in the folder templates
My HTML file head
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
...
<link rel="stylesheet" href="../static/css/main.css" />
<link rel="stylesheet" href="../static/css/styles.css" />
I do not have EnableWebMvc
in any of my Java classes
When I run this as a jar, the page comes up fine, but all the CSS is stripped. It just shows up as plain text. I've tried this in different browsers with the same result. What am I doing wrong?
Upvotes: 1
Views: 972
Reputation: 4683
If you check your browser console (e.g. in Chrome), my guess you will see HTTP 404 Not found for your CSS files. The problem is with your URL path to CSS.
The main point is that there is a difference how files (.html, .css) are stored in your JAR file (or in your source code) and how they are served by HTTP server. As far as I see you are using thymeleafs and therefore (my guess again), your HTML template files can be served for different kind of URLs (like /
, /book/23
etc.) and therefore I don't recommend to address CSS files with relative URLs (as you did).
As CSS files are stored in static
directory, they should be automatically served by spring boot for /**
pattern, ie. if you don't use any context path, your CSS should have following URL: /css/main.css
.
If you want to be very correct and be prepared that your web application can be in different "context", you should use something like <link rel="stylesheet" th:href="@{/css/main.css}">
Upvotes: 3