Common Page Element Display with Java and Mustache

In this post we'll cover how to very easily display common elements like Titles and Page Headings across your website pages with Java and Mustache.

PageInfo Class and Pageinfo.cvs

The trick is using a common object class we'll call PageInfo. Here are the common fields in the PageInfo Class.

We're using an easily maintainable pageinfo.cvs file to store the basic field properties. Here's what that looks like.

An important field here is PAGE_KEY. We'll use that key for determining which row to populate as you'll see in a second.

Populating the PageInfo Object

Here is the primary method of populating the PageInfo Object where pageId serves as our PAGE_KEY value.

Notice we are adding our Shiro User in this method as well so we have easy access to any {{#user}}{{/user}} properties in our templates. It also means we can populate our Mustache Template tags with a single call from any Controller method like in the following example.

Using the PageInfo Object

Here is an example of dynamically activating a Navigation Menu Item using the PageInfo ActiveMenu value based on the NAVBAR_MENU item in our pageinfo.cvs file.

The resulting navigation bar when the ActiveMenu postsMenu property is TRUE.

Here is a more straightforward example populating a page Heading and SubHeading originally stored in the Pageinfo.cvs file we looked at earlier.

So as you can see, using a common Object like PageInfo makes it easy to display common page elements on Mustache templates across your website.

More Like This Post