Now, let's implement a new view to facilitate adding authors. Add the following route to
Next, we need to implement a http post endpoint for the
On this endpoint, we first parse the query parameters to create an author object which we insert into
Authors table using our
Sql2oAuthorDao.add. Then, we check if the author was inserted successfully in which case, in our model, we set
added flag to
ture. Otherwise, we set
failedAdd to true. These
flags are then used in
src/main/resources/templates/addauthor.vm to show appropriate messages to the user. The following is the content of
Now, point your browser to
/addauthor route and try to add an author.
You might have noticed our
Server.main() is getting very long. In general,
Server.main() is not the best place to receive http requests and process them. It would be sensible to move the "controller" code somewhere else. One viable approach is to introduce a new Java package named
src/main/java and create class(es) that contain controller code. Then, we just receive http requests in the
Server class, but then we route those requests to suitable function(s) in the controller classes.
To get more details on Apache Velocity, refer to the user guide.