Post Display by Tag with Spring MVC

A quick post on the cool parts of how we’re displaying NixMash Spring Posts by Tag with Spring MVC. You can give tags a spin on the demo site.

Displaying the View

We’ll begin by looking at the MVC Method for displaying the Post by Tag page view. We’re going to retrieve the Tag object, add it to the model and be on our way. The page has a nice public-facing url of /posts/tag/some+tag.

We’ll use that Tag object in our Thymeleaf-enabled JavaScript to retrieve the posts from our PostRestController MVC class. We have a similar JavaScript function to retrieve additional pages of posts by tag with the “More Posts” button which we won’t show here.

Retrieving the Posts

We’ll start in the PostRestController class where we retrieve a page full of posts. An example url (not shown to the user) would be


If you recall from an earlier post, all NixMash Spring Posts are tested for authorship by the current user, then sent to Velocity for parsing and formatting based on the Post Type of Link, Link Summary, etc.

Paging With Parameters

When we retrieved Posts we retrieved all posts with a PostRepository FindAll(), something like

return postRepository.findAll(pageRequest);

Not much to that, but here we’re adding the TagId parameter and using a custom PostRepository method.

Here’s our Service method where we create the PageRequest.

In the PostRepository we use the abstract Pageable Interface rather than the PageRequest object. The @Query statement is interesting, too, as it demonstrates how to retrieve parent objects based on a @ManyToMany child variable.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.