项目作者: peterszatmary

项目描述 :
Spring Boot demo with Mustache logic less templates and Lombok to keep pojos readable and small.
高级语言: Java
项目地址: git://github.com/peterszatmary/spring-boot-mustache-lombok-demo.git


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

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

How it works

Config

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

  1. package com.szatmary.peter.mustache.demo.conf;
  2. import com.github.mjeanroy.springmvc.view.mustache.configuration.EnableMustache;
  3. import com.github.mjeanroy.springmvc.view.mustache.configuration.MustacheProvider;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  7. @Configuration
  8. @EnableWebMvc
  9. @EnableMustache(provider = MustacheProvider.AUTO)
  10. @ComponentScan("com.szatmary.peter.mustache.demo")
  11. public class MustacheConfig {
  12. }

Controler

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

  1. package com.szatmary.peter.mustache.demo.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import java.util.Map;
  5. import static com.szatmary.peter.mustache.demo.helper.DataHelper.*;
  6. @Controller
  7. public class HelloWorldController {
  8. @RequestMapping("/")
  9. public String mustacheDemo(Map<String, Object> model) {
  10. // one object
  11. model.put("oneStudent", oneStudent());
  12. // two objects
  13. model.put("twoStudents", twoStudents());
  14. // condition
  15. model.put("showIt", true);
  16. // condition
  17. model.put("neverShowIt", false);
  18. // no students
  19. model.put("noStudents", noStudents());
  20. // emptyList
  21. model.put("emptyList", emptyList());
  22. return "demo/helloworld";
  23. }
  24. }

Student object with Lombok

  1. package com.szatmary.peter.mustache.demo.obj;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. @Data
  8. public class Student {
  9. private String name;
  10. private Integer age;
  11. private Address address;
  12. }

Address object with Lombok

  1. package com.szatmary.peter.mustache.demo.obj;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. @AllArgsConstructor
  5. @Data
  6. public class Address {
  7. private String address;
  8. }

Mustache template

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <body>
  4. <h1>Mustache.java + Spring Boot + Lombok demo</h1>
  5. <h2>Render one object</h2>
  6. <strong>oneStudent name:</strong> {{oneStudent.name}}<br />
  7. <strong>oneStudent age:</strong> {{oneStudent.age}}<br />
  8. <strong>oneStudent address:</strong> {{oneStudent.address.address}}<br />
  9. <hr />
  10. <h2>Render two objects</h2>
  11. {{#twoStudents}}
  12. <strong>Name:</strong> {{name}}
  13. <strong>Age:</strong> {{age}}
  14. <strong>Address :</strong> {{address.address}}<br />
  15. {{/twoStudents}}
  16. <hr />
  17. <h2>Render with conditions</h2>
  18. {{#showIt}}
  19. Show it !
  20. {{/showIt}}
  21. {{#neverShowIt}}
  22. Never show it !
  23. {{/neverShowIt}}
  24. <br /><br />
  25. <hr />
  26. <h2>If nothing to render than what ?</h2>
  27. {{#noStudents}}
  28. <strong>Name:</strong> {{name}}
  29. {{/noStudents}}
  30. {{^noStudents}}
  31. No students here.
  32. {{/noStudents}}
  33. <hr />
  34. <h2>Empty list not rendering</h2>
  35. {{#emptyList}}
  36. <strong>Name:</strong> {{name}}<br />
  37. {{/emptyList}}
  38. </body>
  39. </html>