Skip to content

Spring Boot demo with Mustache logic less templates and Lombok to keep pojos readable and small.

Notifications You must be signed in to change notification settings

peterszatmary/spring-boot-mustache-lombok-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-boot-mustache-lombok-demo

Build Status Codacy Badge

Spring Boot demo with Mustache logic less templates integrated with springmvc-mustache. Lombok is used for simplifing POJOs in project.

How to run demo

mvn clean install
java -jar spring-boot-mustache-demo-0.0.1-SNAPSHOT.jar
go to localhost:8080

How it works

Config

Integrated with springmvc-mustache. It is not standard way but very easy one.

package com.szatmary.peter.mustache.demo.conf;

import com.github.mjeanroy.springmvc.view.mustache.configuration.EnableMustache;
import com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheProvider;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@EnableMustache(provider = MustacheProvider.AUTO)
@ComponentScan("com.szatmary.peter.mustache.demo")
public class MustacheConfig {
}

Controler

Map<String, Object> model is used implicitly on template site for rendering your java data from controller.

package com.szatmary.peter.mustache.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
import static com.szatmary.peter.mustache.demo.helper.DataHelper.*;

@Controller
public class HelloWorldController {

    @RequestMapping("/")
    public String mustacheDemo(Map<String, Object> model) {

        // one object
        model.put("oneStudent", oneStudent());
        // two objects
        model.put("twoStudents", twoStudents());
        // condition
        model.put("showIt", true);
        // condition
        model.put("neverShowIt", false);
        // no students
        model.put("noStudents", noStudents());
        // emptyList
        model.put("emptyList", emptyList());

        return "demo/helloworld";
    }
}

Student object with Lombok

package com.szatmary.peter.mustache.demo.obj;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {
    private String name;
    private Integer age;
    private Address address;
}

Address object with Lombok

package com.szatmary.peter.mustache.demo.obj;

import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class Address {
    private String address;
}

Mustache template

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Mustache.java + Spring Boot +  Lombok demo</h1>

    <h2>Render one object</h2>
    <strong>oneStudent name:</strong> {{oneStudent.name}}<br />
    <strong>oneStudent age:</strong> {{oneStudent.age}}<br />
    <strong>oneStudent address:</strong> {{oneStudent.address.address}}<br />

    <hr />

    <h2>Render two objects</h2>
    {{#twoStudents}}
        <strong>Name:</strong> {{name}}
        <strong>Age:</strong> {{age}}
        <strong>Address :</strong> {{address.address}}<br />
    {{/twoStudents}}
    <hr />

    <h2>Render with conditions</h2>

    {{#showIt}}
        Show it !
    {{/showIt}}

    {{#neverShowIt}}
        Never show it !
    {{/neverShowIt}}

    <br /><br />
    <hr />

    <h2>If nothing to render than what ?</h2>

    {{#noStudents}}
        <strong>Name:</strong> {{name}}
    {{/noStudents}}
    {{^noStudents}}
        No students here.
    {{/noStudents}}

    <hr />

    <h2>Empty list not rendering</h2>
    {{#emptyList}}
        <strong>Name:</strong> {{name}}<br />
    {{/emptyList}}
    </body>
</html>

About

Spring Boot demo with Mustache logic less templates and Lombok to keep pojos readable and small.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published