forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#2276 from eugenp/bael-982-mustache-spring
bael-982-mustache-spring
- Loading branch information
Showing
12 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.baeldung</groupId> | ||
<artifactId>spring-mustache</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-mustache</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.4.RELEASE</version> | ||
<relativePath/> | ||
<!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<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> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
34 changes: 34 additions & 0 deletions
34
spring-mustache/src/main/java/com/baeldung/springmustache/SpringMustacheApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
spring-mustache/src/main/java/com/baeldung/springmustache/controller/ArticleController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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<String, Object> model) { | ||
|
||
List<Article> articles = new LinkedList<>(); | ||
IntStream.range(0, 10) | ||
.forEach(count -> { | ||
articles.add(generateArticle("Article Title " + count)); | ||
}); | ||
|
||
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; | ||
} | ||
} | ||
|
||
|
41 changes: 41 additions & 0 deletions
41
spring-mustache/src/main/java/com/baeldung/springmustache/model/Article.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
Empty file.
9 changes: 9 additions & 0 deletions
9
spring-mustache/src/main/resources/templates/error/error.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
|
||
<html lang="en"> | ||
|
||
<body> | ||
Something went wrong: {{status}} {{error}} | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}} |
8 changes: 8 additions & 0 deletions
8
spring-mustache/src/main/resources/templates/layout/article.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div class="starter-template"> | ||
{{#articles}} | ||
<h1>{{title}}</h1> | ||
<h3>{{publishDate}}</h3> | ||
<h3>{{author}}</h3> | ||
<p>{{body}}</p> | ||
{{/articles}} | ||
</div> |
2 changes: 2 additions & 0 deletions
2
spring-mustache/src/main/resources/templates/layout/footer.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!-- this is footer --> | ||
</html> |
11 changes: 11 additions & 0 deletions
11
spring-mustache/src/main/resources/templates/layout/header.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 --> |
30 changes: 30 additions & 0 deletions
30
...ng-mustache/src/test/java/com/baeldung/springmustache/SpringMustacheApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
|
||
} |