10

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
10
  • 2
    When using Spring Boot, if you add you resources to /src/main/resources/static you need any of the addResourceHandlers configuration. Spring Boot will take care of that stuff on it's own if of course you have used @EnableAutoConfiguration and have not used @EnableWebMvc
    – geoand
    Commented May 19, 2014 at 9:17
  • hello geoand, i had used under one configuration class the @EnableWebMvc and now i have removed it but still the css are not beeing loaded. I have even comment the addResourceHandlers bean in order to check if those will be registered automatically. can you post a link regarding the the fact that we should not used EnableWebMvc
    – Tito
    Commented May 19, 2014 at 9:40
  • 1
    If you take a look at the source code of WebMvcAutoConfiguration you will see @ConditionalOnMissingBean(WebMvcConfigurationSupport.class). That means that WebMvcAutoConfiguration is only used when WebMvcConfigurationSupport has not been configured as a bean, which in turn means when no @EnableWebMvc is present
    – geoand
    Commented May 19, 2014 at 9:43
  • If you still have problems, could you post your Spring configuration? I have used the resources the way I mentioned in my first comment and have no problem at all
    – geoand
    Commented May 19, 2014 at 9:45
  • Hello geoand It is stil not what i expected it to be. I will chop down the project and post the project in couple of hours on the github, then i will post the link here. Thank you for your support.
    – Tito
    Commented May 19, 2014 at 9:48

1 Answer 1

13

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

4
  • Hello geoand , this is indeed working, i placed the static folder under the /src/main/resources/ directory and it all worked, however please help me understand what would be the correct path to use refering to the spring reference guide, would that me /src/main/resources/static/css or /src/main/static/css ? . if the path is /src/main/static/css then what would be the correct link to use i.e. <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/syncServer.css}" href="../css/syncServer.css" />
    – Tito
    Commented May 19, 2014 at 15:05
  • thank you for the answer and for the support during this question
    – Tito
    Commented May 19, 2014 at 15:05
  • @Tito Your welcome! Check out docs.spring.io/spring-boot/docs/1.0.2.RELEASE/reference/…. It mentions the supported paths (relative to /resources).
    – geoand
    Commented May 19, 2014 at 16:21
  • english is not my native language but if i understand this correctly it will be /src/main/resources/static , /src/main/resources/resources , /src/main/resources/public ,, i am just a little bit confused because of the double resources folder in the second case
    – Tito
    Commented May 21, 2014 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.