Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put static files for Spark Web Framework?

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.

like image 665
creatsin Avatar asked Jan 24 '15 13:01

creatsin


People also ask

Does spark use Jetty?

Spark is a compact framework for building web applications that run on the JVM. It comes with an embedded web server, Jetty, so you can get started in minutes.

What is a spark route?

In the Spark lingo, a route is a handler A route is a URL pattern that is mapped to a handler. A handler can be a physical file or a $ gradle run. We run the application with gradle run command. An embedded Jetty server is started. $ curl localhost:4567/hello Hello there!

Does spark support Java 17?

It's easy to run locally on one machine — all you need is to have java installed on your system PATH , or the JAVA_HOME environment variable pointing to a Java installation. Spark runs on Java 8/11/17, Scala 2.12/2.13, Python 3.7+ and R 3.5+.

How do I stop Java spark Server?

Stopping the Server By calling the stop() method the server is stopped and all routes are cleared.


1 Answers

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"); 
like image 53
Pablo Matias Gomez Avatar answered Sep 28 '22 04:09

Pablo Matias Gomez