Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Boot ResourceLocations not adding the css file resulting in 404

Well i have a working spring-boot app that is running on a local computer just fine. However I noticed that when i do mvn package then none of my css or java scripts, locates in

/src/main/wepapp/css

are being copied into the jar file (package) created in the target directory.

spring boot reference guide says

65.3 Convert an existing application to Spring Boot "Static resources can be moved to /public (or /static or /resources or /META-INF/resources) in the classpath root."

24.1.4 Static Content "Do not use the src/main/webapp folder if your application will be packaged as a jar. Although this folder is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar."

So that means that i can put all my js and css folders into the folder

/src/main/resources/static

i.e. now my structure looks like that

/src/main/resources/static/css/
/src/main/resources/static/js/

all of my thymeleaf templates however are still located in

/src/main/resources/templates/

I did that, and as far as i understand know i need to add the ResourceHandler to my ResourceHandlerRegistry. Previously when all of my ccs were in "/src/main/wepapp/css/" my ResourceHandlers looked like that and it worked very well for me.

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/pdfs/**").addResourceLocations("/pdfs/").setCachePeriod(0);
    registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(0);
    registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(0);
    registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0);

}

I have tried adding multiple handlers like

 registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(0);

or

 registry.addResourceHandler("/css/**").addResourceLocations("/static/css/").setCachePeriod(0);

or

 registry.addResourceHandler("/css/**").addResourceLocations("/").setCachePeriod(0);

etc.

but none of them worked for me. The html templates are displayed but the web browser console is reporing 404 when trying to locate /css/corresponing.css or /js/corresponing.js

I have deliberately disabled Spring security in my test project, in order to simplify debugging of this problem.

One more thing thing that i do not completely understand is the deployment assembly. I have read an article that said that when i do want to have particular folders into my target package jar file generated by maven, i do need to include those folder into my deployment assembly, well i did however "mvn package" is still not copping all of the content(inlcuding subfolders) of my /src/main/static folder into the target jar file. I see however the "templates" folder copied into the jar file. So there is some other magic happening behind the scene.

enter image description here

Here is how do i declare the css in my thymeleaf layout i.e.

/src/main/resources/templates/layout.html


<!DOCTYPE html>
<html>
  <head>
    <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
    <link rel="stylesheet" type="text/css" media="all"  th:href="@{/css/syncServer.css}"  href="../css/syncServer.css" />
    ...
  </head>
  <body>
      ...
  </body>
</html>

My question is: Is the configuration i done so far correct and if so what other options/settings i need to be aware of in order to make the app find the css files locates in /src/main/static/css/

Addition one

test project

[email protected]:TheDictator/sArchitecture.git
like image 601
Tito Avatar asked May 19 '14 09:05

Tito


1 Answers

If you move you the whole static directory into the resources and totally remove the addResourceHandlers configuration, then everything works fine.

That means that resources structure would look like the following image:

enter image description here

like image 157
geoand Avatar answered Nov 01 '22 18:11

geoand