For those not following along at home, we first introduced NixMash Spring Posts, then focused on the role of Jsoup in generating the Page Preview form, and most recently, how to create a Bootstrap Carousel with Spring MVC and Thymeleaf.
You can view NixMash Spring Posts on the demo site, but here’s a screenshot to save time. What makes the display of the posts interesting is that they can be one of several display types. Examples of display types are Links with Text, Links with a Thumbnail Image and Featured Links with a Large Image.
You would normally think to use JSON for something like this, but with the dynamic nature of the display we pass formatted HTML to the client. Below are the two Controller methods used to generate the stream. Notice we’re producing “text/html” rather than “application/json”. We’ll take a look at both the postService.getPosts() and templateService.createPostHtml() methods next.
Here is our postService.getPosts() method which is so awesome because it shows the efficiency of Spring JPA Paging.
Generating the HTML Display
We call on the services of Apache Velocity to display the individual posts based on their DisplayType property. Here’s our template service where we merge the specific Velocity Template with our Post to generate the HTML.
Here’s a sample Velocity Template.