Question

Base on this tutorial:

http://spring.io/guides/gs/serving-web-content/

I can use the thymeleaf to serve the view in the location

/src/main/resources/templates/

However, I have to put the static web contents (css, js) in another location:

/src/main/webapp/resources

And linking the resources in the hello.html like that:

<link href="resources/hello.css" />
<script src="resources/hello.js"></script>

The directory structure is:

└── src
    └── main
        └── java
            └── hello.java
        └──resources
            └──templates
               └──hello.html
        └──webapp
            └──resources
               └──hello.js
               └──hello.css

The problem is that the links of the static files are work when I run the web server. But if I open the html files in offline mode, the links are broken.

Could I move the static resource from

/src/main/webapp/resources

to:

/src/main/resources/templates/resources

The new directory structure would be like:

└── src
    └── main
        └── java
            └── hello.java
        └──resources
            └──templates
               └──hello.html
               └──resources
                  └──hello.js
                  └──hello.css

I tried it but is doesn't work.

Was it helpful?

Solution

Try src/main/resources/static (or src/main/resources/public or src/main/resources/resources). All those are registered by Spring Boot autoconfig.

OTHER TIPS

Define the mapping in your context like following -

<mvc:resources mapping="/templates/**"
     location="classpath:/templates/" />

This will route everything that's hitting /template/ url to search for contents under your src/main/resources/templates/ folder.

In case you need tweaking in the url/prefix - adjust the mapping accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top