A hello world with spark:
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 file index.html instead?
Notes:
I know I am very late to the party, You can do the following:
staticFiles.location("/public");
// create a folder called 'public'
under 'src/main/resources'
folder
When the app is initialized, call the above method before any of the routes
or requests
. This is very important.
In your "controller"
, you can add it like this:
response.redirect("test.html"); return null;
You can do so by passing the absolute path to your static resources 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 your index.html file in the root of your static resources directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With