Where do I put files when trying to serve static files with the Spark web framework?
I haven't been able to find anything online - I'm beginning to suspect I don't understand anything about class paths, relative paths etc. for an Eclipse and Java project.
This paragraph about static files in Spark refers to /public
, but I have no idea where that would be. Using windows, Eclipse Luna and my project is converted to use Maven.
I've tried looking at the code on GitHub, but I'm a little out of my depth trying to find it.
src/main/resources
Spark.staticFileLocation("/public");
toSpark.staticFileLocation("public");
First you have to tell Spark where to search for the static files like this:
In Spark versions prior to 2.5, you should use:
Then your project should have a
public
folder under the resources folder like this/src/main/resources/public/style.css
For example I added a
style.css
file there, so you should then access it like this:http://localhost:4567/style.css
If you want to serve a non-classpath folder, then you should use
In Spark versions prior to 2.5, you should use:
Right click your project on Eclipse, select create New -> Package. Give the new package a name, etc.
Put your static resources under that package, so we can be sure they're under your classpath.
I put my style sheets below my static content as follows:
And the index.html
I also have other generated HTML pages as with freemarker. They just collect the path:
Shows the CSS way index gets it.
Source: https://groups.google.com/d/msg/sparkjava/5vMuK_5GEBU/vh_jHra75u0J