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.
seinecle
  • 10,118
  • 14
  • 61
  • 120

2 Answers2

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;

UVM
  • 9,776
  • 6
  • 41
  • 66
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.

Laercio Metzner
  • 1,351
  • 11
  • 22
  • Thanks! And then, how do I return this file in the get method? – seinecle Dec 12 '15 at 14:17
  • All the static files are returned through the get method. To see your index.html, just open your browser and type http://localhost:4567 – Laercio Metzner Dec 16 '15 at 01:28
  • 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? – code Dec 17 '15 at 19:21
  • 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"). – Laercio Metzner Dec 17 '15 at 23:49
  • 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 (http://sparkjava.com/documentation.html) and you will notice this is much simpler than JSP and JSF. – Laercio Metzner Dec 17 '15 at 23:57
  • 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. – pgollangi Jun 06 '16 at 11:59
  • @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. – Laercio Metzner Jun 06 '16 at 17:56