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?