From dc0d802cd905d6c15632524ab2e4ebc3e6bb561f Mon Sep 17 00:00:00 2001 From: dhruba619 Date: Sun, 16 Jul 2017 12:36:17 +0530 Subject: [PATCH 1/2] BAEL-982 Spring-boot mustache initial commit --- spring-mustache/.gitignore | 24 ++++++++ spring-mustache/pom.xml | 60 +++++++++++++++++++ .../SpringMustacheApplication.java | 34 +++++++++++ .../controller/ArticleController.java | 50 ++++++++++++++++ .../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 | 32 ++++++++++ 12 files changed, 280 insertions(+) create mode 100644 spring-mustache/.gitignore create mode 100644 spring-mustache/pom.xml create mode 100644 spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java create mode 100644 spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java create mode 100644 spring-mustache/src/main/java/com/baeldung/springmustache/model/Article.java create mode 100644 spring-mustache/src/main/resources/application.properties create mode 100644 spring-mustache/src/main/resources/templates/error/error.html create mode 100644 spring-mustache/src/main/resources/templates/index.html create mode 100644 spring-mustache/src/main/resources/templates/layout/article.html create mode 100644 spring-mustache/src/main/resources/templates/layout/footer.html create mode 100644 spring-mustache/src/main/resources/templates/layout/header.html create mode 100644 spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java diff --git a/spring-mustache/.gitignore b/spring-mustache/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-mustache/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-mustache/pom.xml b/spring-mustache/pom.xml new file mode 100644 index 0000000000..e671672b72 --- /dev/null +++ b/spring-mustache/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + com.baeldung + spring-mustache + 0.0.1-SNAPSHOT + jar + + spring-mustache + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + 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 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java b/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java new file mode 100644 index 0000000000..d4338ffd7d --- /dev/null +++ b/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java @@ -0,0 +1,34 @@ +package com.baeldung.springmustache; + +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; + +import com.samskivert.mustache.Mustache; + +@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); + + Mustache.Compiler compiler = Mustache.compiler() + .defaultValue("Some Default Value") + .withLoader(templateLoader) + .withCollector(collector); + return compiler; + + } +} + diff --git a/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java b/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java new file mode 100644 index 0000000000..e0db127d66 --- /dev/null +++ b/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java @@ -0,0 +1,50 @@ +package com.baeldung.springmustache.controller; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.stream.IntStream; + +import org.fluttercode.datafactory.impl.DataFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.servlet.ModelAndView; + +import com.baeldung.springmustache.model.Article; + +@Controller +public class ArticleController { + + @RequestMapping("/article") + public ModelAndView displayArticle(Map model) { + + List
articles = new LinkedList<>(); + IntStream.range(0, 10) + .forEach(count -> { + articles.add(generateArticle("Article Title "+count)); + }); + + 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/spring-mustache/src/main/java/com/baeldung/springmustache/model/Article.java b/spring-mustache/src/main/java/com/baeldung/springmustache/model/Article.java new file mode 100644 index 0000000000..78b08f877f --- /dev/null +++ b/spring-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/spring-mustache/src/main/resources/application.properties b/spring-mustache/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-mustache/src/main/resources/templates/error/error.html b/spring-mustache/src/main/resources/templates/error/error.html new file mode 100644 index 0000000000..fa29db41c4 --- /dev/null +++ b/spring-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/spring-mustache/src/main/resources/templates/index.html b/spring-mustache/src/main/resources/templates/index.html new file mode 100644 index 0000000000..bda60f9d8f --- /dev/null +++ b/spring-mustache/src/main/resources/templates/index.html @@ -0,0 +1,9 @@ +{{>layout/header}} + +
{{>layout/article}}
+ + + + +{{>layout/footer}} diff --git a/spring-mustache/src/main/resources/templates/layout/article.html b/spring-mustache/src/main/resources/templates/layout/article.html new file mode 100644 index 0000000000..9d573580d3 --- /dev/null +++ b/spring-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/spring-mustache/src/main/resources/templates/layout/footer.html b/spring-mustache/src/main/resources/templates/layout/footer.html new file mode 100644 index 0000000000..04f34cac54 --- /dev/null +++ b/spring-mustache/src/main/resources/templates/layout/footer.html @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/spring-mustache/src/main/resources/templates/layout/header.html b/spring-mustache/src/main/resources/templates/layout/header.html new file mode 100644 index 0000000000..d203ef800b --- /dev/null +++ b/spring-mustache/src/main/resources/templates/layout/header.html @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java b/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java new file mode 100644 index 0000000000..4a72d4156e --- /dev/null +++ b/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java @@ -0,0 +1,32 @@ +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")); + + } + +} From 59cb7db53e660ac324411476fd18213098d5db88 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Mon, 17 Jul 2017 19:12:24 +0100 Subject: [PATCH 2/2] BAEL-982 - reformatting --- spring-mustache/pom.xml | 97 ++++++++++--------- .../SpringMustacheApplication.java | 8 +- .../controller/ArticleController.java | 12 +-- .../SpringMustacheApplicationTests.java | 6 +- 4 files changed, 60 insertions(+), 63 deletions(-) diff --git a/spring-mustache/pom.xml b/spring-mustache/pom.xml index e671672b72..45fee6b10e 100644 --- a/spring-mustache/pom.xml +++ b/spring-mustache/pom.xml @@ -1,60 +1,61 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.baeldung - spring-mustache - 0.0.1-SNAPSHOT - jar + com.baeldung + spring-mustache + 0.0.1-SNAPSHOT + jar - spring-mustache - Demo project for Spring Boot + spring-mustache + Demo project for Spring Boot - - org.springframework.boot - spring-boot-starter-parent - 1.5.4.RELEASE - - + + org.springframework.boot + spring-boot-starter-parent + 1.5.4.RELEASE + + + - - UTF-8 - UTF-8 - 1.8 - + + UTF-8 + UTF-8 + 1.8 + - - - org.springframework.boot - spring-boot-starter-mustache - + + + 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 - - + + org.springframework.boot + spring-boot-starter-test + test + + + org.webjars + bootstrap + 3.3.7 + + + org.fluttercode.datafactory + datafactory + 0.8 + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java b/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java index d4338ffd7d..addd1fa088 100644 --- a/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java +++ b/spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java @@ -10,7 +10,7 @@ import org.springframework.core.env.Environment; import com.samskivert.mustache.Mustache; @SpringBootApplication -@ComponentScan(basePackages = { "com.baeldung" }) +@ComponentScan(basePackages = {"com.baeldung"}) public class SpringMustacheApplication { public static void main(String[] args) { @@ -24,9 +24,9 @@ public class SpringMustacheApplication { collector.setEnvironment(environment); Mustache.Compiler compiler = Mustache.compiler() - .defaultValue("Some Default Value") - .withLoader(templateLoader) - .withCollector(collector); + .defaultValue("Some Default Value") + .withLoader(templateLoader) + .withCollector(collector); return compiler; } diff --git a/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java b/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java index e0db127d66..b24625e7d5 100644 --- a/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java +++ b/spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java @@ -23,9 +23,9 @@ public class ArticleController { List
articles = new LinkedList<>(); IntStream.range(0, 10) - .forEach(count -> { - articles.add(generateArticle("Article Title "+count)); - }); + .forEach(count -> { + articles.add(generateArticle("Article Title " + count)); + }); Map modelMap = new HashMap<>(); modelMap.put("articles", articles); @@ -38,12 +38,10 @@ public class ArticleController { 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()); + "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/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java b/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java index 4a72d4156e..9138dfe92b 100644 --- a/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java +++ b/spring-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java @@ -22,11 +22,9 @@ public class SpringMustacheApplicationTests { 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")); + Assert.assertTrue(entity.getStatusCode().equals(HttpStatus.OK)); + Assert.assertTrue(entity.getBody().contains("Article Title 0")); } }