How to return a static html page with Spark Java?
Asked Answered
R

2

10

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 need this index.html to be in the jar
  • in the spirit of simplicity of spark java, I'd like to avoid as much as possible going through templates, that would be overkill for a static page.
Rossetti answered 10/12, 2015 at 9:2 Comment(0)
F
8

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.

Fainthearted answered 11/12, 2015 at 9:59 Comment(7)
Thanks! And then, how do I return this file in the get method?Rossetti
All the static files are returned through the get method. To see your index.html, just open your browser and type localhost:4567Fainthearted
I had a few questions. In Spark Java, where's the static resource directory? Also, if a route is handling /hello/, should an index.html file be placed in /hello/? Lastly, can we use a .JSP file?Foremost
If you're used to JSP programming, SparkJava is a lot more freestyle than that. It does not provide a default location for static files, like the WEB-CONTENT directory in a JSP project scructure (on eclipse). Instead, you have to provide this directory. If the directory is inside the classpath of your application, use staticFileLocation("/yourpath"). If it's outside of your project, point to it using externalStaticFileLocation("/yourfullpath").Fainthearted
Declaring new routes, like /hello/, has nothing to do with creating subdirectories and stuff. It's all done in your java code, inside any initialization method (can even be the main(String[] args)), do it like this: get("/hello", (req, res) -> "hello"). Follow the documentation (sparkjava.com/documentation.html) and you will notice this is much simpler than JSP and JSF.Fainthearted
If I choose to write JSP, what should my approach to dispatch request to my JSP. Because It is not same as HTML as it needs JSP compiler to convert that page to HTML.Aorta
@Prasana Kumar, you sould post a new question so you can get help from jsp experts. by the way, I think you could add the SparkJava dependency to your project and send requests from your front-end to your server with jquery.Fainthearted
C
10

I know I am very late to the party, You can do the following:

  1. staticFiles.location("/public"); // create a folder called 'public' under 'src/main/resources' folder

  2. When the app is initialized, call the above method before any of the routes or requests. This is very important.

  3. In your "controller", you can add it like this:

response.redirect("test.html"); return null;

Cicisbeo answered 28/11, 2016 at 6:31 Comment(1)
The beauty of SO is that you are never late to the party.Holliehollifield
F
8

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.

Fainthearted answered 11/12, 2015 at 9:59 Comment(7)
Thanks! And then, how do I return this file in the get method?Rossetti
All the static files are returned through the get method. To see your index.html, just open your browser and type localhost:4567Fainthearted
I had a few questions. In Spark Java, where's the static resource directory? Also, if a route is handling /hello/, should an index.html file be placed in /hello/? Lastly, can we use a .JSP file?Foremost
If you're used to JSP programming, SparkJava is a lot more freestyle than that. It does not provide a default location for static files, like the WEB-CONTENT directory in a JSP project scructure (on eclipse). Instead, you have to provide this directory. If the directory is inside the classpath of your application, use staticFileLocation("/yourpath"). If it's outside of your project, point to it using externalStaticFileLocation("/yourfullpath").Fainthearted
Declaring new routes, like /hello/, has nothing to do with creating subdirectories and stuff. It's all done in your java code, inside any initialization method (can even be the main(String[] args)), do it like this: get("/hello", (req, res) -> "hello"). Follow the documentation (sparkjava.com/documentation.html) and you will notice this is much simpler than JSP and JSF.Fainthearted
If I choose to write JSP, what should my approach to dispatch request to my JSP. Because It is not same as HTML as it needs JSP compiler to convert that page to HTML.Aorta
@Prasana Kumar, you sould post a new question so you can get help from jsp experts. by the way, I think you could add the SparkJava dependency to your project and send requests from your front-end to your server with jquery.Fainthearted

© 2022 - 2024 — McMap. All rights reserved.