Skip to content

Commit 0bb7f2b

Browse files
authored
BAEL-8033: Getting Started with Compile-Time Templates With Spring (#19105)
1 parent e9b9cbd commit 0bb7f2b

29 files changed

Lines changed: 670 additions & 1 deletion

File tree

spring-web-modules/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<modules>
1919
<module>spring-5-mvc</module>
2020
<module>spring-freemarker</module>
21+
<module>spring-java-templates</module>
2122
<module>spring-mvc-basics</module>
2223
<module>spring-mvc-basics-2</module>
2324
<module>spring-mvc-basics-3</module>
@@ -79,4 +80,4 @@
7980
</plugins>
8081
</build>
8182

82-
</project>
83+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-java-templates-jstachio</artifactId>
7+
<name>spring-java.templates.jstachio</name>
8+
<packaging>jar</packaging>
9+
<description>Spring Java Templates Module - JStachio</description>
10+
11+
<parent>
12+
<groupId>com.baeldung.web</groupId>
13+
<artifactId>spring-java-templates</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-test</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
28+
<!-- JStachio -->
29+
<dependency>
30+
<groupId>io.jstach</groupId>
31+
<artifactId>jstachio</artifactId>
32+
<version>${jstachio.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.jstach</groupId>
36+
<artifactId>jstachio-spring-boot-starter-webmvc</artifactId>
37+
<version>${jstachio.version}</version>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
49+
<plugin>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<configuration>
52+
<compilerArgs>
53+
<arg>-parameters</arg>
54+
</compilerArgs>
55+
<annotationProcessorPaths>
56+
<path>
57+
<groupId>io.jstach</groupId>
58+
<artifactId>jstachio-apt</artifactId>
59+
<version>${jstachio.version}</version>
60+
</path>
61+
</annotationProcessorPaths>
62+
</configuration>
63+
</plugin>
64+
65+
</plugins>
66+
</build>
67+
68+
<properties>
69+
<start-class>com.baeldung.templates.SpringJavaTemplatesApplication</start-class>
70+
<spring-boot.version>3.5.7</spring-boot.version>
71+
<logback.version>1.5.17</logback.version>
72+
<java.version>21</java.version>
73+
74+
<jstachio.version>1.3.7</jstachio.version>
75+
</properties>
76+
</project>
77+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.templates;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.servlet.View;
6+
7+
import io.jstach.opt.spring.webmvc.JStachioModelView;
8+
9+
@Controller
10+
public class JStachioController {
11+
@GetMapping("/jstachio")
12+
public View get() {
13+
return JStachioModelView.of(new JStachioModel("JStachio"));
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.templates;
2+
3+
import io.jstach.jstache.JStache;
4+
5+
@JStache(path = "templates/jstachio/jstachio.mustache")
6+
public record JStachioModel(String name) {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.templates;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringJavaTemplatesApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringJavaTemplatesApplication.class, args);
11+
}
12+
13+
}
14+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
Hello, {{name}}!
4+
</body>
5+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.templates;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class JStachioUnitTest {
8+
@Test
9+
void whenRenderingJStachio_thenTheOutputIsCorrect() {
10+
JStachioModel model = new JStachioModel("Baeldung");
11+
12+
StringBuilder sb = new StringBuilder();
13+
JStachioModelRenderer.of().execute(model, sb);
14+
15+
assertEquals("""
16+
<html>
17+
<body>
18+
Hello, Baeldung!
19+
</body>
20+
</html>
21+
""", sb.toString());
22+
}
23+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>spring-java-templates-jte</artifactId>
7+
<name>spring-java.templates.jte</name>
8+
<packaging>jar</packaging>
9+
<description>Spring Java Templates Module - JTE</description>
10+
11+
<parent>
12+
<groupId>com.baeldung.web</groupId>
13+
<artifactId>spring-java-templates</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-test</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
28+
<!-- JTE -->
29+
<dependency>
30+
<groupId>gg.jte</groupId>
31+
<artifactId>jte</artifactId>
32+
<version>${jte.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>gg.jte</groupId>
36+
<artifactId>jte-spring-boot-starter-3</artifactId>
37+
<version>${jte.version}</version>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
49+
<plugin>
50+
<groupId>gg.jte</groupId>
51+
<artifactId>jte-maven-plugin</artifactId>
52+
<version>${jte.version}</version>
53+
<configuration>
54+
<sourceDirectory>${basedir}/src/main/resources/templates/jte</sourceDirectory>
55+
<targetDirectory>${basedir}/target/jte</targetDirectory>
56+
<contentType>Html</contentType>
57+
</configuration>
58+
<executions>
59+
<execution>
60+
<phase>generate-sources</phase>
61+
<goals>
62+
<goal>generate</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
68+
</plugins>
69+
</build>
70+
71+
<properties>
72+
<start-class>com.baeldung.templates.SpringJavaTemplatesApplication</start-class>
73+
<spring-boot.version>3.5.7</spring-boot.version>
74+
<logback.version>1.5.17</logback.version>
75+
<java.version>21</java.version>
76+
77+
<jte.version>3.2.1</jte.version>
78+
</properties>
79+
</project>
80+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.templates;
2+
3+
import java.util.Map;
4+
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
9+
@Controller
10+
public class JteController {
11+
@GetMapping("/jte")
12+
public String view(Model model) {
13+
model.addAttribute("model", new JteModel("JTE"));
14+
return "JteDemo";
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.templates;
2+
3+
public record JteModel(String name) {
4+
}

0 commit comments

Comments
 (0)