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
Please enter at least three characters to search
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Scalable System Design: Core Concepts for Building Reliable Software
  • Accelerating AI Inference With TensorRT
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  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.7K 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
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!