Where to put static files for Spark Web Framework?
Asked Answered
M

4

32

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.

Messmate answered 24/1, 2015 at 13:21 Comment(0)
W
55

First you have to tell Spark where to search for the static files like this:

Spark.staticFiles.location("/public");

In Spark versions prior to 2.5, you should use:

Spark.staticFileLocation("/public");

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

Spark.staticFiles.externalLocation("/path/to/dir");

In Spark versions prior to 2.5, you should use:

Spark.externalStaticFileLocation("/path/to/dir");
Wahkuna answered 30/1, 2015 at 0:58 Comment(12)
@JigarJoshi what issue are you talking about? Does my solution not work for you?Wahkuna
I ran into decision quickly, I was trying to set proper mime type for static resources I serve, I ended up using a Filter to set proper mime type for certain static resourcesTaintless
@JigarJoshi well if you serve the static files using staticFileLocation then you dont have to do that. If you serve the files manually registering a get with Spark.get then yes, you will have to set the headers manually.Wahkuna
I was serving css file using staticFileLocation and the mime type was not set properlyTaintless
@JigarJoshi maybe you were using an old version of Spark? I never heard of anyone having that problem :/Wahkuna
I am using 2.3 which is latest as of now I believeTaintless
where should I place the user uploaded image,it's the same as the src/main/resources?Pirate
@L.YanJun what are you talking about? This question is about serving static files, not user's files.Wahkuna
@PabloMatiasGomez When I use get("/post/:postId", (req, res) ->{...}), the static file path become such like http://0.0.0.0:4567/post/css/style.css, but the correct path should be http://0.0.0.0:4567/css/style.css.Pirate
What if I want to serve my entire repository, not just the public folder? I've tried staticFiles.location(repoPath) but it is not getting picked up? Does it only work with public folder? How can I access any file I want in the current directory?Schnitzler
No, the public folder is an example. The folder has to be in the classpath. If you want to use a non-classpath, then you can use staticFiles.externalLocation. I updated the anser with this.Wahkuna
The problem is the assets links in the HTML/template files will be broken. I like to design my pages as I go and do not want to go back in the template files to change the CSS/JS file links. Spark breaks the paths and I don't like it.Pless
R
3

I put my style sheets below my static content as follows:

staticFileLocation( "/web" );
/web/
  |-- index.html
  +-- styles/
        +
        +--- default.css

And the index.html

... <link href="styles/default.css" rel="stylesheet"   type="text/css" />

I also have other generated HTML pages as with freemarker. They just collect the path:

  • /styles/default.css, or
  • localhost:8081/styles/default.css

Shows the CSS way index gets it.

Source: https://groups.google.com/d/msg/sparkjava/5vMuK_5GEBU/vh_jHra75u0J

Rancher answered 8/2, 2015 at 2:11 Comment(0)
C
2
  1. Right click your project on Eclipse, select create New -> Package. Give the new package a name, etc.

  2. Put your static resources under that package, so we can be sure they're under your classpath.

  3. In your Main class colde, call staticFileLocation("yourpackagename/");
Charmian answered 27/3, 2015 at 15:8 Comment(0)
T
1
  1. Place your public directory into src/main/resources
  2. Replace Spark.staticFileLocation("/public"); to Spark.staticFileLocation("public");
Transcendent answered 12/8, 2017 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.