techwasti
Published in

techwasti

Spring-Boot Mustache Hello World Example.

In this article, we will demonstrate to you how to develop a Mustache template technology with a spring-boot.

Below is the high-level technology we are using.

Let us jump into the https://start.spring.io/ and generate the project with the below starters.

spring-boot-starter-web and spring-boot-starter-mustache

build.gradle

plugins {id 'org.springframework.boot' version '2.4.1'id 'io.spring.dependency-management' version '1.0.10.RELEASE'id 'java'}group = 'com.techwasti.mustache'version = '0.0.1-SNAPSHOT'sourceCompatibility = '11'repositories {mavenCentral()}dependencies {implementation 'org.springframework.boot:spring-boot-starter-mustache'implementation 'org.springframework.boot:spring-boot-starter-web'testImplementation 'org.springframework.boot:spring-boot-starter-test'}test {useJUnitPlatform()}

Mustache:-

Mustache is a simple web template system. It is available for many programming languages including Java. Mustache is described as logic-less because it does not have any explicit control flow statements, such as if and else conditionals or for loops. Looping and conditional evaluation can be achieved using section tags processing lists and lambdas.

Project Structure:

Download the project from start.spring.io and open it in your favorite editor mine is visual studio code.

As we mentioned here let us go ahead and create a simple mustache template file.

hello.mustache

<!DOCTYPE HTML><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><title>Spring Boot Mustache Hello World Example</title></head><body><h2>{{message}}</h2></body></html>

note:- Mustache template file extension must be ".mustache"

Now let us write controller code that will pass the placeholder value.

HelloController.java

package com.techwasti.springmustacheex;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;@Controllerpublic class HelloController {@GetMapping(value = {"/", "/hello"})public String greetMessage(Model model) {String helloworld="Hello and welcome to mustache";model.addAttribute("message", helloworld);return "hello";}}

Here we have mentioned /hello and / request mapping to the greetMessage method that will return the view with hello name. ViewResolver will resolve this view name with hello.mustache from the template folder. The attribute which we have added with "message" as a key will be rendered in the mustache file.

Now our code is ready, you can go ahead and start the spring application. Once an application is up hit the URL http://localhost:8080/ and you will be able to see the below output.

If you want to pass the parameter then use @RequestParam annotation.

@RequestParam(name = “name”, required = true, defaultValue = “springboot”)

Try out these and let me know your experience.

Source Code:-

https://github.com/maheshwarLigade/spring-boot-examples
$ git clone https://github.com/maheshwarLigade/spring-boot-examples
$ cd spring-mustache-ex
$ gradle build
$ gradle bootRun

Conclusion:

Spring Boot is really the most popular framework in the java community to develop microservices, cloud, or web applications. Mustache is a logic-less template engine and it’s a popular one to develop web applications.

More such articles:-

https://medium.com/techwasti

Let’s connect on Stackoverflow, LinkedIn, Facebook & Twitter.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maheshwar Ligade

Learner, Full Stack Developer, blogger, amateur #ML,#DL,#AI dev in the quantum moment. I run https://techwasti.com/ to post all my articles.