How to return static html page using Spark Java?

World world with sparks:

get(new Route("/hello") { @Override public Object handle(Request request, Response response) { response.type("text/html"); return "<h1>Hello Spark MVC Framework!</h1>"; } }); 

How can I return a static index.html file?

Notes:

  • I need this index.html to be in the bank
  • in the spirit of the simplicity of spark java, I would like to avoid as many patterns as possible, this would be unnecessary for a static page.
+6
source share
2 answers

You can do this by passing the absolute path to the static resource directory in this method:

 externalStaticFileLocation("/var/www/public"); 

Or by passing the relative path in this method:

 staticFileLocation("/public"); 

Call this before setting any route. Create an index.html file in the root directory of your static resources.

+4
source

I know that I'm very late to the party, you can do the following:

  • staticFiles.location("/public"); // create a folder called 'public' in the folder 'src/main/resources'

  • When the application is initialized, call the above method before any of the routes or requests . It is very important.

  • In the "controller" you can add it like this:

Response.Redirect ("test.html"); return null;

+5
source

All Articles