Skip to content

Commit 2f1bb7b

Browse files
Merge pull request eugenp#2569 from eugenp/BAEL-982-mustache-spring-boot
BAEL-982 - Moving to mustache module
2 parents 4c73a0c + 39fc396 commit 2f1bb7b

11 files changed

Lines changed: 223 additions & 3 deletions

File tree

‎mustache/pom.xml‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
<name>mustache</name>
99

1010
<parent>
11-
<groupId>com.baeldung</groupId>
12-
<artifactId>parent-modules</artifactId>
13-
<version>1.0.0-SNAPSHOT</version>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.5.4.RELEASE</version>
14+
<relativePath/>
15+
<!-- lookup parent from repository -->
1416
</parent>
1517

1618
<dependencies>
@@ -32,6 +34,28 @@
3234
<artifactId>log4j</artifactId>
3335
<version>${log4j.version}</version>
3436
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-mustache</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.webjars</groupId>
50+
<artifactId>bootstrap</artifactId>
51+
<version>3.3.7</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.fluttercode.datafactory</groupId>
55+
<artifactId>datafactory</artifactId>
56+
<version>0.8</version>
57+
</dependency>
58+
3559
<dependency>
3660
<groupId>junit</groupId>
3761
<artifactId>junit</artifactId>
@@ -41,6 +65,15 @@
4165

4266
</dependencies>
4367

68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
4477
<properties>
4578
<mustache.compiler.api.version>0.9.2</mustache.compiler.api.version>
4679
<assertj.version>3.7.0</assertj.version>
@@ -52,6 +85,8 @@
5285
<!-- maven plugins -->
5386
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
5487
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
88+
89+
<java.version>1.8</java.version>
5590
</properties>
5691

5792
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.springmustache;
2+
3+
import com.samskivert.mustache.Mustache;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.mustache.MustacheEnvironmentCollector;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.core.env.Environment;
10+
11+
@SpringBootApplication
12+
@ComponentScan(basePackages = {"com.baeldung"})
13+
public class SpringMustacheApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(SpringMustacheApplication.class, args);
17+
}
18+
19+
@Bean
20+
public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader templateLoader, Environment environment) {
21+
22+
MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector();
23+
collector.setEnvironment(environment);
24+
25+
return Mustache.compiler()
26+
.defaultValue("Some Default Value")
27+
.withLoader(templateLoader)
28+
.withCollector(collector);
29+
30+
}
31+
}
32+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.springmustache.controller;
2+
3+
import com.baeldung.springmustache.model.Article;
4+
import org.fluttercode.datafactory.impl.DataFactory;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.servlet.ModelAndView;
8+
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.IntStream;
14+
15+
@Controller
16+
public class ArticleController {
17+
18+
@GetMapping("/article")
19+
public ModelAndView displayArticle(Map<String, Object> model) {
20+
21+
List<Article> articles = IntStream.range(0, 10)
22+
.mapToObj(i -> generateArticle("Article Title " + i))
23+
.collect(Collectors.toList());
24+
25+
Map<String, Object> modelMap = new HashMap<>();
26+
modelMap.put("articles", articles);
27+
28+
return new ModelAndView("index", modelMap);
29+
}
30+
31+
private Article generateArticle(String title) {
32+
Article article = new Article();
33+
DataFactory factory = new DataFactory();
34+
article.setTitle(title);
35+
article.setBody(
36+
"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.");
37+
article.setPublishDate(factory.getBirthDate().toString());
38+
article.setAuthor(factory.getName());
39+
return article;
40+
}
41+
}
42+
43+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.springmustache.model;
2+
3+
public class Article {
4+
private String title;
5+
private String body;
6+
private String author;
7+
private String publishDate;
8+
9+
public String getTitle() {
10+
return title;
11+
}
12+
13+
public void setTitle(String title) {
14+
this.title = title;
15+
}
16+
17+
public String getBody() {
18+
return body;
19+
}
20+
21+
public void setBody(String body) {
22+
this.body = body;
23+
}
24+
25+
public String getAuthor() {
26+
return author;
27+
}
28+
29+
public void setAuthor(String author) {
30+
this.author = author;
31+
}
32+
33+
public String getPublishDate() {
34+
return publishDate;
35+
}
36+
37+
public void setPublishDate(String publishDate) {
38+
this.publishDate = publishDate;
39+
}
40+
41+
}

‎mustache/src/main/resources/application.properties‎

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en">
4+
5+
<body>
6+
Something went wrong: {{status}} {{error}}
7+
</body>
8+
9+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{>layout/header}}
2+
<body>
3+
<div class="container">{{>layout/article}}</div>
4+
5+
<script type="text/javascript"
6+
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
7+
8+
</body>
9+
{{>layout/footer}}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="starter-template">
2+
{{#articles}}
3+
<h1>{{title}}</h1>
4+
<h3>{{publishDate}}</h3>
5+
<h3>{{author}}</h3>
6+
<p>{{body}}</p>
7+
{{/articles}}
8+
</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- this is footer -->
2+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE HTML>
2+
<head>
3+
4+
<title></title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6+
7+
<link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="css/main.css" />
9+
10+
</head>
11+
<!-- this is header -->

0 commit comments

Comments
 (0)