Merge pull request #2569 from eugenp/BAEL-982-mustache-spring-boot

BAEL-982 - Moving to mustache module
This commit is contained in:
slavisa-baeldung 2017-09-06 22:54:26 +02:00 committed by GitHub
commit 2f1bb7b47f
11 changed files with 223 additions and 3 deletions

View File

@ -8,9 +8,11 @@
<name>mustache</name> <name>mustache</name>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>parent-modules</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.5.4.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent> </parent>
<dependencies> <dependencies>
@ -32,6 +34,28 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>${log4j.version}</version> <version>${log4j.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mustache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.fluttercode.datafactory</groupId>
<artifactId>datafactory</artifactId>
<version>0.8</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -41,6 +65,15 @@
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties> <properties>
<mustache.compiler.api.version>0.9.2</mustache.compiler.api.version> <mustache.compiler.api.version>0.9.2</mustache.compiler.api.version>
<assertj.version>3.7.0</assertj.version> <assertj.version>3.7.0</assertj.version>
@ -52,6 +85,8 @@
<!-- maven plugins --> <!-- maven plugins -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version> <maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version> <maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<java.version>1.8</java.version>
</properties> </properties>
</project> </project>

View File

@ -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);
}
}

View File

@ -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<String, Object> model) {
List<Article> articles = IntStream.range(0, 10)
.mapToObj(i -> generateArticle("Article Title " + i))
.collect(Collectors.toList());
Map<String, Object> 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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<body>
Something went wrong: {{status}} {{error}}
</body>
</html>

View File

@ -0,0 +1,9 @@
{{>layout/header}}
<body>
<div class="container">{{>layout/article}}</div>
<script type="text/javascript"
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
{{>layout/footer}}

View File

@ -0,0 +1,8 @@
<div class="starter-template">
{{#articles}}
<h1>{{title}}</h1>
<h3>{{publishDate}}</h3>
<h3>{{author}}</h3>
<p>{{body}}</p>
{{/articles}}
</div>

View File

@ -0,0 +1,2 @@
<!-- this is footer -->
</html>

View File

@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
<link rel="stylesheet" href="css/main.css" />
</head>
<!-- this is header -->

View File

@ -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<String> entity = this.restTemplate.getForEntity("/article", String.class);
Assert.assertTrue(entity.getStatusCode().equals(HttpStatus.OK));
Assert.assertTrue(entity.getBody().contains("Article Title 0"));
}
}