1

This is the simplest example where we map a path to a String, so we are responsible to build the whole HTML:

get("/hello", (req, res) -> "Hello World");

We can also use template engines that will build the HTML for us given the ModelAndView:

Map map = new HashMap();
map.put("name", "Sam");

// hello.html file is in resources/templates directory
get("/hello", (rq, rs) -> new ModelAndView(map, "hello"), new ThymeleafTemplateEngine());

Now, what if I want to map a path to a static HTML file, where I don't have any variables to be interpreted by a template engine? I know I could simply use an empty map:

get("/static", (rq, rs) -> new ModelAndView(new HashMap(), "static"), new ThymeleafTemplateEngine());

But then I would be going through the engine's overhead without any reason.

I know I could also read the HTML file and return it as a String, like this gist did. But I feel there might be a cleaner way to do this. Is there?

Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57

1 Answers1

2

You can create a staticfiles location and reference html there directly.

Spark.staticFileLocation("/static");

Create a folder called static under your resources directory.

/src/main/resources/static

Put your static html in this file (e.g., test.html)

Now you can access the files directly via the browser as;

http://localhost:8080/test.html

Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31