From 39fc39615eb0e10547cfc0b7cbf8fbed8aa5f49d Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Wed, 6 Sep 2017 17:35:40 +0100 Subject: [PATCH] BAEL-982 - Moving to mustache module --- mustache/pom.xml | 41 ++++++++++++++++-- .../SpringMustacheApplication.java | 32 ++++++++++++++ .../controller/ArticleController.java | 43 +++++++++++++++++++ .../springmustache/model/Article.java | 41 ++++++++++++++++++ .../src/main/resources/application.properties | 0 .../main/resources/templates/error/error.html | 9 ++++ .../src/main/resources/templates/index.html | 9 ++++ .../resources/templates/layout/article.html | 8 ++++ .../resources/templates/layout/footer.html | 2 + .../resources/templates/layout/header.html | 11 +++++ .../SpringMustacheApplicationTests.java | 30 +++++++++++++ 11 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java create mode 100644 mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java create mode 100644 mustache/src/main/java/com/baeldung/springmustache/model/Article.java create mode 100644 mustache/src/main/resources/application.properties create mode 100644 mustache/src/main/resources/templates/error/error.html create mode 100644 mustache/src/main/resources/templates/index.html create mode 100644 mustache/src/main/resources/templates/layout/article.html create mode 100644 mustache/src/main/resources/templates/layout/footer.html create mode 100644 mustache/src/main/resources/templates/layout/header.html create mode 100644 mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java diff --git a/mustache/pom.xml b/mustache/pom.xml index d5d80b58e9..15c0e3a8c7 100644 --- a/mustache/pom.xml +++ b/mustache/pom.xml @@ -8,9 +8,11 @@ mustache - com.baeldung - parent-modules - 1.0.0-SNAPSHOT + org.springframework.boot + spring-boot-starter-parent + 1.5.4.RELEASE + + @@ -32,6 +34,28 @@ log4j ${log4j.version} + + + org.springframework.boot + spring-boot-starter-mustache + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.webjars + bootstrap + 3.3.7 + + + org.fluttercode.datafactory + datafactory + 0.8 + + junit junit @@ -41,6 +65,15 @@ + + + + org.springframework.boot + spring-boot-maven-plugin + + + + 0.9.2 3.7.0 @@ -52,6 +85,8 @@ 3.6.0 2.19.1 + + 1.8 \ No newline at end of file diff --git a/mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java b/mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java new file mode 100644 index 0000000000..8cdf89d08a --- /dev/null +++ b/mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java @@ -0,0 +1,32 @@ +package com.baeldung.springmustache; + +import com.samskivert.mustache.Mustache; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.mustache.MustacheEnvironmentCollector; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.env.Environment; + +@SpringBootApplication +@ComponentScan(basePackages = {"com.baeldung"}) +public class SpringMustacheApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringMustacheApplication.class, args); + } + + @Bean + public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader templateLoader, Environment environment) { + + MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector(); + collector.setEnvironment(environment); + + return Mustache.compiler() + .defaultValue("Some Default Value") + .withLoader(templateLoader) + .withCollector(collector); + + } +} + diff --git a/mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java b/mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java new file mode 100644 index 0000000000..5fc34c9f07 --- /dev/null +++ b/mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java @@ -0,0 +1,43 @@ +package com.baeldung.springmustache.controller; + +import com.baeldung.springmustache.model.Article; +import org.fluttercode.datafactory.impl.DataFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +@Controller +public class ArticleController { + + @GetMapping("/article") + public ModelAndView displayArticle(Map model) { + + List
articles = IntStream.range(0, 10) + .mapToObj(i -> generateArticle("Article Title " + i)) + .collect(Collectors.toList()); + + Map modelMap = new HashMap<>(); + modelMap.put("articles", articles); + + return new ModelAndView("index", modelMap); + } + + private Article generateArticle(String title) { + Article article = new Article(); + DataFactory factory = new DataFactory(); + article.setTitle(title); + article.setBody( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur faucibus tempor diam. In molestie arcu eget ante facilisis sodales. Maecenas porta tellus sapien, eget rutrum nisi blandit in. Mauris tempor auctor ante, ut blandit velit venenatis id. Ut varius, augue aliquet feugiat congue, arcu ipsum finibus purus, dapibus semper velit sapien venenatis magna. Nunc quam ex, aliquet at rutrum sed, vestibulum quis libero. In laoreet libero cursus maximus vulputate. Nullam in fermentum sem. Duis aliquam ullamcorper dui, et dictum justo placerat id. Aliquam pretium orci quis sapien convallis, non blandit est tempus."); + article.setPublishDate(factory.getBirthDate().toString()); + article.setAuthor(factory.getName()); + return article; + } +} + + diff --git a/mustache/src/main/java/com/baeldung/springmustache/model/Article.java b/mustache/src/main/java/com/baeldung/springmustache/model/Article.java new file mode 100644 index 0000000000..78b08f877f --- /dev/null +++ b/mustache/src/main/java/com/baeldung/springmustache/model/Article.java @@ -0,0 +1,41 @@ +package com.baeldung.springmustache.model; + +public class Article { + private String title; + private String body; + private String author; + private String publishDate; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getPublishDate() { + return publishDate; + } + + public void setPublishDate(String publishDate) { + this.publishDate = publishDate; + } + +} diff --git a/mustache/src/main/resources/application.properties b/mustache/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/mustache/src/main/resources/templates/error/error.html b/mustache/src/main/resources/templates/error/error.html new file mode 100644 index 0000000000..fa29db41c4 --- /dev/null +++ b/mustache/src/main/resources/templates/error/error.html @@ -0,0 +1,9 @@ + + + + + + Something went wrong: {{status}} {{error}} + + + \ No newline at end of file diff --git a/mustache/src/main/resources/templates/index.html b/mustache/src/main/resources/templates/index.html new file mode 100644 index 0000000000..bda60f9d8f --- /dev/null +++ b/mustache/src/main/resources/templates/index.html @@ -0,0 +1,9 @@ +{{>layout/header}} + +
{{>layout/article}}
+ + + + +{{>layout/footer}} diff --git a/mustache/src/main/resources/templates/layout/article.html b/mustache/src/main/resources/templates/layout/article.html new file mode 100644 index 0000000000..9d573580d3 --- /dev/null +++ b/mustache/src/main/resources/templates/layout/article.html @@ -0,0 +1,8 @@ +
+ {{#articles}} +

{{title}}

+

{{publishDate}}

+

{{author}}

+

{{body}}

+ {{/articles}} +
\ No newline at end of file diff --git a/mustache/src/main/resources/templates/layout/footer.html b/mustache/src/main/resources/templates/layout/footer.html new file mode 100644 index 0000000000..04f34cac54 --- /dev/null +++ b/mustache/src/main/resources/templates/layout/footer.html @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/mustache/src/main/resources/templates/layout/header.html b/mustache/src/main/resources/templates/layout/header.html new file mode 100644 index 0000000000..d203ef800b --- /dev/null +++ b/mustache/src/main/resources/templates/layout/header.html @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java b/mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java new file mode 100644 index 0000000000..9138dfe92b --- /dev/null +++ b/mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java @@ -0,0 +1,30 @@ +package com.baeldung.springmustache; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class SpringMustacheApplicationTests { + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void givenIndexPageWhenContainsArticleThenTrue() { + + ResponseEntity entity = this.restTemplate.getForEntity("/article", String.class); + + Assert.assertTrue(entity.getStatusCode().equals(HttpStatus.OK)); + Assert.assertTrue(entity.getBody().contains("Article Title 0")); + } + +}