Close

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.

Example

Maven dependencies

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>spring-boot-mustache-example</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Mustache Template File

/src/main/resources/templates/forex-view.html

<html>
<head>
    <style>
table {
    border-collapse: collapse;
    width:100%;
}
table, td, th {
    border: 1px solid #999;
    padding: 5px;
}
</style>
</head>
<body>
<h2>Currency Rates</h2>
<table>
    <thead>
    <tr>
        <th>
            <lablel>Currency Pair</lablel>
        </th>
        <th>
            <lablel>Bid Price</lablel>
        </th>
        <th>
            <lablel>Ask Price</lablel>
        </th>
        <th>
            <lablel>Date</lablel>
        </th>
    </tr>
    </thead>
    <tbody>
    {{#todayCurrencyRates}}
    <tr>
        <td>
            {{currencyPair}}
        </td>
        <td>
            {{askPrice}}
        </td>
        <td>
            {{bidPrice}}
        </td>
        <td>
            {{dateTime}}
        </td>
    </tr>
    {{/todayCurrencyRates}}
    </tbody>
</table>
</body>
</html>

Spring MVC Controller

@Controller
@RequestMapping("/")
public class MyController {

  @RequestMapping
  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 = LocalDateTime.now();
      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();
          cr.setCurrencyPair(currencyPair);
          cr.setDateTime(today);
          BigDecimal bidPrice = new BigDecimal(Math.random() * 5 + 1);
          bidPrice = bidPrice.setScale(3, RoundingMode.CEILING);
          cr.setBidPrice(bidPrice);
          BigDecimal askPrice = new BigDecimal(bidPrice.doubleValue() + Math.random() * 2 + 0.5);
          askPrice = askPrice.setScale(3, RoundingMode.CEILING);
          cr.setAskPrice(askPrice);
          currencyRates.add(cr);
      }
      return currencyRates;
  }
}

Spring boot main class

@SpringBootApplication
public class ExampleMain {

  public static void main(String[] args) throws InterruptedException {
      SpringApplication.run(ExampleMain.class, 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.

Output

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