Where to put static files for Spark Web Framework?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 05:48:48

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");

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

  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/");
Moses
  1. Place your public directory into src/main/resources
  2. Replace Spark.staticFileLocation("/public"); to Spark.staticFileLocation("public");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!