Spring Boot - Using Mustache View

[Last Updated: Jan 26, 2018]

To use Mustache in Spring Boot application, we just need to include spring-boot-starter-mustache dependency and place the template file under src/main/resources/templates/ directory. The rest of the configurations is done automatically by Spring Boot. Also check out our Mustache example with plain MVC.


Maven dependencies


<project .....>

Mustache Template File


table {
    border-collapse: collapse;
table, td, th {
    border: 1px solid #999;
    padding: 5px;
<h2>Currency Rates</h2>
            <lablel>Currency Pair</lablel>
            <lablel>Bid Price</lablel>
            <lablel>Ask Price</lablel>

Spring MVC Controller

public class MyController {

  public String handleRequest(Model model) {
      model.addAttribute("todayCurrencyRates", getTodayForexRates());
      return "forex-view";

  private List<CurrencyRate> getTodayForexRates() {
      //dummy rates
      List<CurrencyRate> currencyRates = new ArrayList<>();
      LocalDateTime today =;
      List<Currency> currencies = new ArrayList<>(Currency.getAvailableCurrencies());

      for (int i = 0; i < currencies.size(); i += 2) {
          String currencyPair = currencies.get(i) + "/" + currencies.get(i + 1);
          CurrencyRate cr = new CurrencyRate();
          BigDecimal bidPrice = new BigDecimal(Math.random() * 5 + 1);
          bidPrice = bidPrice.setScale(3, RoundingMode.CEILING);
          BigDecimal askPrice = new BigDecimal(bidPrice.doubleValue() + Math.random() * 2 + 0.5);
          askPrice = askPrice.setScale(3, RoundingMode.CEILING);
      return currencyRates;

Spring boot main class

public class ExampleMain {

  public static void main(String[] args) throws InterruptedException {, args);

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

We can also run the main class from our IDE.


Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.5.9.RELEASE
    Corresponding Spring Version 4.3.13.RELEASE
  • spring-boot-starter-mustache : Starter for building MVC web applications using Mustache views.
  • JDK 1.8
  • Maven 3.3.9

Spring Boot - Mustache View Example Select All Download
  • spring-boot-mustache-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • templates
            • forex-view.html

    See Also