1

What is the easiest way to use a straight HTML page as a Spark template (IE, I don't want to go through a TemplateEngine implementation).

I can use a template engine fine, like so:

Spark.get("/test", (req, res) -> new ModelAndView(map, "template.html"), new MustacheTemplateEngine());

And I tried just using the ModelAndView without an Engine:

Spark.get("/", (req, res) -> new ModelAndView(new HashMap(), "index.html"));

But that get's me just the toString() of the model and view: spark.ModelAndView@3bdadfd8.

I am thinking of writing my own engine and implementing render() to do the IO to serve the html file, but is there a better way?

rene
  • 41,474
  • 78
  • 114
  • 152
mtyson
  • 8,196
  • 16
  • 66
  • 106

3 Answers3

7

You don't want a template engine. All you want is to get the content of the HTML files.

Spark.get("/", (req, res) -> renderContent("index.html"));

...

private String renderContent(String htmlFile) {
    new String(Files.readAllBytes(Paths.get(getClass().getResource(htmlFile).toURI())), StandardCharsets.UTF_8);
}
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
6

Updated Answer

With the help of the answer provided here, I have updated this answer.

get("/", (req, res) -> renderContent("index.html"));

...

private String renderContent(String htmlFile) {
    try {
        // If you are using maven then your files
        // will be in a folder called resources.
        // getResource() gets that folder
        // and any files you specify.
        URL url = getClass().getResource(htmlFile);

        // Return a String which has all
        // the contents of the file.
        Path path = Paths.get(url.toURI());
        return new String(Files.readAllBytes(path), Charset.defaultCharset());
    } catch (IOException | URISyntaxException e) {
        // Add your own exception handlers here.
    }
    return null;
}

Old Answer

Unfortunately, I have found no other solution than to override the render() method in your own TemplateEngine. Please note the following uses Java NIO for reading in the contents of a file:

public class HTMLTemplateEngine extends TemplateEngine {

    @Override
    public String render(ModelAndView modelAndView) {
        try {
            // If you are using maven then your files
            // will be in a folder called resources.
            // getResource() gets that folder 
            // and any files you specify.
            URL url = getClass().getResource("public/" + 
                        modelAndView.getViewName());

            // Return a String which has all
            // the contents of the file.
            Path path = Paths.get(url.toURI());
            return new String(Files.readAllBytes(path), Charset.defaultCharset());
        } catch (IOException | URISyntaxException e) {
            // Add your own exception handlers here.
        }
        return null;
     }
}

Then, in your route you call the HTMLTemplateEngine as follows:

// Render index.html on homepage
get("/", (request, response) -> new ModelAndView(new HashMap(), "index.html"),
 new HTMLTemplateEngine());
Community
  • 1
  • 1
gurpreet-
  • 509
  • 8
  • 18
  • Nicely done. You should send a pull request to the Spark project. – mtyson Nov 19 '15 at 15:22
  • 1
    This looks wrong. If you just want the HTML content to be rendered, then you're not using a template, therefore it doesn't make sense to create a `TemplateEngine`. Your `HTMLTemplateEngine` is not an actual template engine. And in the end you end up with the weird `new HashMap()` that does nothing more than pollute the code. – Fred Porciúncula Dec 01 '15 at 15:03
  • 1
    Your logic is sound, I have updated my answer to reflect this. – gurpreet- Dec 01 '15 at 18:26
2

Another One Line solution that can help:

get("/", (q, a) -> IOUtils.toString(Spark.class.getResourceAsStream("/path/to/index.html")));

You need this import: import spark.utils.IOUtils;

source : https://github.com/perwendel/spark/issues/550

Tobliug
  • 2,992
  • 30
  • 28