Template Engine
This is the WebServer source file as it stands now:
public class WebServer {
public static void main(String[] args) {
get("/", (req, res) -> "<h1>Welcome to CourseSearch UI!</h1>");
}
}
Although the browser is able to identify the <h1></h1>
HTML tag and appropriately format the welcome message, our HTML is invalid (or rather incomplete). An HTML document must have the following minimal structure:
<html>
<head>
<!-- meta data about the page -->
</head>
<body>
<!-- content of the page -->
</body>
</html>
The welcome message will have to go inside the <body></body>
element. Moreover, there are some meta-data information that are highly recommended to be included in an HTML file. So, a minimal HTML to contain our welcome message would need to contain this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JHU Course Search</title>
</head>
<body>
<h1>Welcome to CourseSearch UI!</h1>
</body>
</html>
HTML is verbose; it is a lot to type out (in a Java source code) to send out data to a client. So, are we just going to have to concatenate a bunch of strings in our route method? That would work, but it sure would be hard to maintain all those plus signs and new lines. We can use a template engine as a way around this.
A template engine enables you to use static template files and dynamically update them based on data at runtime.
Spark allows you to plug in several different templating engines. We are going to use one called handlebars.