DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Spring Beans With Auto-Generated Implementations: How-To
  • Introduction to Spring Boot and JDBCTemplate: JDBC Template
  • JobRunr and Spring Data
  • How Spring and Hibernate Simplify Web and Database Management

Trending

  • Server-Driven UI: Agile Interfaces Without App Releases
  • Automating E2E Tests With MFA: Streamline Your Testing Workflow
  • Testing the MongoDB MCP Server Using SingleStore Kai
  • Building V1 Gen-AI Products at Scale: Technical Product Patterns That Work
  1. DZone
  2. Coding
  3. Frameworks
  4. Using Handlebars Java as a Server Side Templating Language

Using Handlebars Java as a Server Side Templating Language

The disconnect between a programming language and how media is rendered has always caused problems. Handlebars is one of the best solutions on the market right now for Java devs building web apps.

By 
Siva Prasad Rao Janapati user avatar
Siva Prasad Rao Janapati
·
Jul. 21, 16 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
27.8K Views

Join the DZone community and get the full member experience.

Join For Free

{{ Handlebars.java }} - Mustache templates in Java

              handlebars.java is a server side Java template engine like Velocity or Freemaker. It follows the syntax of the Mustache spec. The main goal of handlebar.java is to reuse the same templates on both the client and server side. In this article we will see how to use Handlebars.java along with the Spring framework. I have used Spring Boot for this demonstration. Follow the below steps to setup and run handlebars.java

Step 1

Include the handlebars.java implementation in the pom.xml

<dependency>
    <groupId>com.github.jknack</groupId>
    <artifactId>handlebars</artifactId>
    <version>4.0.5</version>
</dependency>

As we are going to use JSON as the data to be rendered with handlebars template, we will include the handlebars JSON helper in the class path.

<dependency>
    <groupId>com.github.jknack</groupId>
    <artifactId>handlebars-jackson2</artifactId>
    <version>4.0.5</version>
</dependency>
<dependency>
    <groupId>com.github.jknack</groupId>
    <artifactId>handlebars-guava-cache</artifactId>
    <version>4.0.5</version>
</dependency>

Step 2

Write handlebar template to render home page. Here, I divided the home page template into header and footer and included them as partials.

{{>header/header}}
This is the sample template to demonstrate the Handlebars JAVA. This uses JSON to render.
{{>footer/footer address}}

The header template is given below.

<h1>Hi, This is {{title}} {{name}}</h1>

The footer template is given below.

The address is given below.
<h4>address1:{{address1}}</h4>
<h4>address2:{{address2}}</h4>
<h4>city:{{city}}</h4>
<h4>state :{{state}}</h4>

Step 3

As we are using Spring Boot, I kept the templates under resources/templates folder. Load the templates from the calsspath.

TemplateLoader loader = new ClassPathTemplateLoader("/templates", ".hbs");
final Cache<TemplateSource, Template> templateCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(1000).build();
setHandlebars(new Handlebars(loader).with((new GuavaTemplateCache(templateCache))));

Step 4

Compile the template and apply the data to render.

Template template = this.getHandlebars().compile(templateName);
//Sample JSON to render the home template
String responseJson = "{\"title\":\"Mr.\",\"name\":\"ABC\",\"address\":{\"address1\":\"address1\",\"address2\":\"address2\",\"city\":\"XYZ\",\"state\":\"State1\"}}";
JsonNode jsonNode = new ObjectMapper().readValue(responseJson, JsonNode.class);
//get the compiled template
Template template = handlebarsDemoTemplateLoader.getTemplate("home");
//Apply the JSON data by passing the context
template.apply(handlebarsDemoTemplateLoader.getContext(jsonNode));

I included the above code in org.smarttechie.controller.HandlebarsDemoController . Just run the org.smarttechie.HandlebarsjavaDemoApplication and send the request to  /demo/home to see the rendered home page handlebar template. The source code created for this example is available on GitHub. Download it and explore more about the handlebars java implementation.

Spring Boot Handlebars Java Demonstration

Java (programming language) Spring Framework Template

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Beans With Auto-Generated Implementations: How-To
  • Introduction to Spring Boot and JDBCTemplate: JDBC Template
  • JobRunr and Spring Data
  • How Spring and Hibernate Simplify Web and Database Management

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: