Add an author
Now, let's implement a new view to facilitate adding authors. Add the following route to Server.main:
Next, we need to implement a http post endpoint for the /addauthor route:
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 addauthor.vm file:
Now, point your browser to /addauthor route and try to add an author.
Controller Code
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 controller under 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.
info
To get more details on Apache Velocity, refer to the user guide.