Skip to content

Commit 597372d

Browse files
committed
split mvc to two projects
1 parent 777b039 commit 597372d

39 files changed

+1051
-52
lines changed

boot-freemarker/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@
4242
<groupId>org.springframework.boot</groupId>
4343
<artifactId>spring-boot-starter-webflux</artifactId>
4444
</dependency>
45-
<dependency>
45+
<!-- <dependency>
4646
<groupId>org.springframework.boot</groupId>
4747
<artifactId>spring-boot-starter-freemarker</artifactId>
48+
</dependency>-->
49+
50+
<dependency>
51+
<groupId>org.freemarker</groupId>
52+
<artifactId>freemarker</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.springframework</groupId>
56+
<artifactId>spring-context-support</artifactId>
4857
</dependency>
4958
<dependency>
5059
<groupId>org.projectlombok</groupId>

boot-freemarker/src/main/java/com/example/demo/DemoApplication.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import lombok.NoArgsConstructor;
99
import lombok.ToString;
1010
import lombok.extern.slf4j.Slf4j;
11+
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.boot.CommandLineRunner;
1213
import org.springframework.boot.SpringApplication;
1314
import org.springframework.boot.autoconfigure.SpringBootApplication;
15+
import org.springframework.context.ApplicationContext;
1416
import org.springframework.context.annotation.Bean;
17+
import org.springframework.context.annotation.Configuration;
1518
import org.springframework.data.annotation.CreatedDate;
1619
import org.springframework.data.annotation.Id;
1720
import org.springframework.data.mongodb.config.EnableMongoAuditing;
@@ -38,6 +41,8 @@
3841
import org.springframework.web.bind.annotation.RequestBody;
3942
import org.springframework.web.bind.annotation.RequestMapping;
4043
import org.springframework.web.bind.annotation.RestController;
44+
import org.springframework.web.reactive.result.view.freemarker.FreeMarkerConfigurer;
45+
import org.springframework.web.reactive.result.view.freemarker.FreeMarkerViewResolver;
4146
import reactor.core.publisher.Flux;
4247
import reactor.core.publisher.Mono;
4348

@@ -51,6 +56,30 @@ public static void main(String[] args) {
5156

5257
}
5358

59+
@Configuration
60+
class WebConfig {
61+
62+
@Autowired
63+
ApplicationContext ctx;
64+
65+
@Bean
66+
public FreeMarkerConfigurer freeMarkerConfig() {
67+
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
68+
configurer.setPreferFileSystemAccess(false);
69+
configurer.setTemplateLoaderPath("classpath:/templates/");
70+
configurer.setResourceLoader(this.ctx);
71+
return configurer;
72+
}
73+
74+
@Bean
75+
public FreeMarkerViewResolver freeMarkerViewResolver() {
76+
final FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver("", ".ftl");
77+
freeMarkerViewResolver.setOrder(1);
78+
return freeMarkerViewResolver;
79+
}
80+
81+
}
82+
5483
@EnableWebFluxSecurity
5584
class SecurityConfig {
5685

@@ -115,23 +144,24 @@ public void run(String[] args) {
115144
}
116145

117146
@Controller
118-
class HomeController{
147+
class HomeController {
148+
119149
private final PostRepository posts;
120150

121151
public HomeController(PostRepository posts) {
122152
this.posts = posts;
123153
}
124-
154+
125155
@GetMapping("/home")
126-
public String home(Model model){
156+
public String home(Model model) {
127157
model.addAttribute("posts", this.posts.findAll().collectList().block(Duration.ofSeconds(100)));
128158
return "home";
129159
}
130-
160+
131161
@GetMapping("/hello")
132-
public String hello(Model model){
162+
public String hello(Model model) {
133163
model.addAttribute("hello", "Hi, Freemarker");
134-
return "hell";
164+
return "hello";
135165
}
136166
}
137167

boot-freemarker/src/main/resources/templates/home.ftl

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,12 @@
99
<body>
1010
<h1>All posts</h1>
1111
<div>
12-
<table>
13-
<thead>
14-
<tr>
15-
<th> ID</th>
16-
<th>Title </th>
17-
<th>Content</th>
18-
</tr>
19-
</thead>
20-
<tbody>
2112

22-
<#list posts as post>
23-
<tr>
24-
<td>${post.id}</td>
25-
<td>${post.title}</td>
26-
<td>${post.content}</td>
27-
</tr>
28-
<#else>
29-
nothing
30-
</#list>
31-
32-
</tbody>
33-
</table>
13+
<#list posts as post>
14+
post : ${post} <br>
15+
<#else>
16+
nothing
17+
</#list>
3418

3519
</div>
3620
</body>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

mvc-freemarker/pom.xml

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>spring-reactive-sample-mvc-freemarker</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-reactive-sample-mvc-freemarker</name>
12+
<description>Spring Webflux MVC Freemarker demo(without Spring Boot)</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.M3</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.data</groupId>
30+
<artifactId>spring-data-mongodb</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.mongodb</groupId>
34+
<artifactId>mongodb-driver-reactivestreams</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework</groupId>
39+
<artifactId>spring-context</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-context-support</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-webflux</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.thymeleaf.extras</groupId>
52+
<artifactId>thymeleaf-extras-java8time</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.thymeleaf</groupId>
56+
<artifactId>thymeleaf-spring5</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.freemarker</groupId>
61+
<artifactId>freemarker</artifactId>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>com.fasterxml.jackson.core</groupId>
66+
<artifactId>jackson-databind</artifactId>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>io.netty</groupId>
71+
<artifactId>netty-buffer</artifactId>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>io.projectreactor.ipc</groupId>
76+
<artifactId>reactor-netty</artifactId>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.projectlombok</groupId>
81+
<artifactId>lombok</artifactId>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>org.slf4j</groupId>
86+
<artifactId>slf4j-api</artifactId>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.slf4j</groupId>
90+
<artifactId>jcl-over-slf4j</artifactId>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.slf4j</groupId>
94+
<artifactId>jul-to-slf4j</artifactId>
95+
</dependency>
96+
<dependency>
97+
<groupId>ch.qos.logback</groupId>
98+
<artifactId>logback-core</artifactId>
99+
</dependency>
100+
<dependency>
101+
<groupId>ch.qos.logback</groupId>
102+
<artifactId>logback-classic</artifactId>
103+
</dependency>
104+
105+
<dependency>
106+
<groupId>junit</groupId>
107+
<artifactId>junit</artifactId>
108+
<scope>test</scope>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.springframework</groupId>
112+
<artifactId>spring-test</artifactId>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.skyscreamer</groupId>
117+
<artifactId>jsonassert</artifactId>
118+
<scope>test</scope>
119+
</dependency>
120+
<dependency>
121+
<groupId>io.projectreactor</groupId>
122+
<artifactId>reactor-test</artifactId>
123+
<scope>test</scope>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.springframework.security</groupId>
127+
<artifactId>spring-security-test</artifactId>
128+
<scope>test</scope>
129+
</dependency>
130+
</dependencies>
131+
132+
<build>
133+
<plugins>
134+
<!-- Maven Assembly Plugin -->
135+
<plugin>
136+
<groupId>org.apache.maven.plugins</groupId>
137+
<artifactId>maven-assembly-plugin</artifactId>
138+
<configuration>
139+
<descriptorRefs>
140+
<descriptorRef>jar-with-dependencies</descriptorRef>
141+
</descriptorRefs>
142+
<!-- MainClass in mainfest make a executable jar -->
143+
<archive>
144+
<manifest>
145+
<mainClass>com.example.demo.Application</mainClass>
146+
</manifest>
147+
</archive>
148+
149+
</configuration>
150+
<executions>
151+
<execution>
152+
<id>make-assembly</id>
153+
<phase>package</phase> <!-- bind to the packaging phase -->
154+
<goals>
155+
<goal>single</goal>
156+
</goals>
157+
</execution>
158+
</executions>
159+
</plugin>
160+
161+
162+
</plugins>
163+
</build>
164+
165+
<repositories>
166+
<repository>
167+
<id>spring-snapshots</id>
168+
<name>Spring Snapshots</name>
169+
<url>https://repo.spring.io/snapshot</url>
170+
<snapshots>
171+
<enabled>true</enabled>
172+
</snapshots>
173+
</repository>
174+
<repository>
175+
<id>spring-milestones</id>
176+
<name>Spring Milestones</name>
177+
<url>https://repo.spring.io/milestone</url>
178+
<snapshots>
179+
<enabled>false</enabled>
180+
</snapshots>
181+
</repository>
182+
</repositories>
183+
184+
<pluginRepositories>
185+
<pluginRepository>
186+
<id>spring-snapshots</id>
187+
<name>Spring Snapshots</name>
188+
<url>https://repo.spring.io/snapshot</url>
189+
<snapshots>
190+
<enabled>true</enabled>
191+
</snapshots>
192+
</pluginRepository>
193+
<pluginRepository>
194+
<id>spring-milestones</id>
195+
<name>Spring Milestones</name>
196+
<url>https://repo.spring.io/milestone</url>
197+
<snapshots>
198+
<enabled>false</enabled>
199+
</snapshots>
200+
</pluginRepository>
201+
</pluginRepositories>
202+
203+
204+
</project>

mvc/src/main/java/com/example/demo/HomeController.java renamed to mvc-freemarker/src/main/java/com/example/demo/HomeController.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ public class HomeController {
3131
@GetMapping("/home")
3232
public String home(final Model model) {
3333

34-
Flux<Post> postList = this.posts.findAll();
35-
model.addAttribute("posts", new ReactiveDataDriverContextVariable(postList, 100));
36-
return "home";
37-
}
38-
39-
@GetMapping("/freemarker")
40-
public String freemarker(final Model model) {
41-
4234
Flux<Post> fluxPost = this.posts.findAll();
4335
List<Post> postList = fluxPost.collectList().block(Duration.ofDays(1));
4436
//log.info(" post list chuncked size::" + postList.size());
4537
model.addAttribute("posts", postList);
46-
return "freemarker";
38+
return "home";
4739
}
4840
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.example.demo;
7+
8+
import lombok.AllArgsConstructor;
9+
import lombok.Builder;
10+
import lombok.Data;
11+
import lombok.NoArgsConstructor;
12+
import lombok.ToString;
13+
import org.springframework.data.annotation.Id;
14+
import org.springframework.data.mongodb.core.mapping.Document;
15+
16+
/**
17+
*
18+
* @author hantsy
19+
*/
20+
@Data
21+
@ToString
22+
@Builder
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
@Document
26+
public class Post {
27+
28+
@Id
29+
private String id;
30+
private String title;
31+
private String content;
32+
33+
}

0 commit comments

Comments
 (0)