Skip to content

Commit 374a1b8

Browse files
committed
add spring-boot-web-thymeleaf
1 parent 8bc1baf commit 374a1b8

File tree

18 files changed

+541
-2
lines changed

18 files changed

+541
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准
1717
- [spring-boot-banner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):Spring Boot 定制 Banner 示例
1818
- [spring-boot-docker](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):使用 Docker 部署 Spring Boot 示例
1919
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql 示例
20-
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Spring Boot 使用 commandLineRunner 实现项目启动时资源初始化示例
20+
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Spring Boot 使用 commandLineRunner 实现项目启动时资源初始化示例
21+
- [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf) :Spring Boot 使用 thymeleaf 实现增删改查示例
2122

2223

2324
**参考文章**

README_EN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Spring Boot Examples, Use the simplest and most useful scene demo.
1616
- [spring-boot-banner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):Spring Boot Customized Banner
1717
- [spring-boot-docker](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-banner):Spring Boot with Docker
1818
- [dockercompose-springboot-mysql-nginx](https://github.com/ityouknow/spring-boot-examples/tree/master/dockercompose-springboot-mysql-nginx) :Docker Compose + Spring Boot + Nginx + Mysql example
19-
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Example of resource initialization at project startup using Spring Boot and commandLineRunner
19+
- [spring-boot-commandLineRunner](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-commandLineRunner) :Example of resource initialization at project startup using Spring Boot and commandLineRunner
20+
- [spring-boot-web-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-web-thymeleaf) :Spring Boot uses thymeleaf to implement addition, deletion, and modification
21+
2022

2123
---
2224

spring-boot-web-thymeleaf/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
<artifactId>spring-boot-web-thymeleaf</artifactId>
6+
<name>Spring Boot Web thymeleaf Sample</name>
7+
<description>Spring Boot Web thymeleaf Sample</description>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>2.0.0.RELEASE</version>
13+
</parent>
14+
15+
<dependencies>
16+
<!-- Compile -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
24+
</dependency>
25+
<!-- Test -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
3+
package com.neo;
4+
5+
import com.neo.model.Message;
6+
import com.neo.repository.InMemoryMessageRepository;
7+
import com.neo.repository.MessageRepository;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.core.convert.converter.Converter;
12+
13+
@SpringBootApplication
14+
public class ThymeleafApplication {
15+
16+
@Bean
17+
public MessageRepository messageRepository() {
18+
return new InMemoryMessageRepository();
19+
}
20+
21+
@Bean
22+
public Converter<String, Message> messageConverter() {
23+
return new Converter<String, Message>() {
24+
@Override
25+
public Message convert(String id) {
26+
return messageRepository().findMessage(Long.valueOf(id));
27+
}
28+
};
29+
}
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(ThymeleafApplication.class, args);
33+
}
34+
35+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
package com.neo.controller;
3+
4+
import javax.validation.Valid;
5+
6+
import com.neo.model.Message;
7+
import com.neo.repository.MessageRepository;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.validation.BindingResult;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.ModelAttribute;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.servlet.ModelAndView;
16+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
17+
18+
@Controller
19+
@RequestMapping("/")
20+
public class MessageController {
21+
22+
private final MessageRepository messageRepository;
23+
24+
public MessageController(MessageRepository messageRepository) {
25+
this.messageRepository = messageRepository;
26+
}
27+
28+
@GetMapping
29+
public ModelAndView list() {
30+
Iterable<Message> messages = this.messageRepository.findAll();
31+
return new ModelAndView("messages/list", "messages", messages);
32+
}
33+
34+
@GetMapping("{id}")
35+
public ModelAndView view(@PathVariable("id") Message message) {
36+
return new ModelAndView("messages/view", "message", message);
37+
}
38+
39+
@GetMapping(params = "form")
40+
public String createForm(@ModelAttribute Message message) {
41+
return "messages/form";
42+
}
43+
44+
@PostMapping
45+
public ModelAndView create(@Valid Message message, BindingResult result,
46+
RedirectAttributes redirect) {
47+
if (result.hasErrors()) {
48+
return new ModelAndView("messages/form", "formErrors", result.getAllErrors());
49+
}
50+
message = this.messageRepository.save(message);
51+
redirect.addFlashAttribute("globalMessage", "Successfully created a new message");
52+
return new ModelAndView("redirect:/{message.id}", "message.id", message.getId());
53+
}
54+
55+
@RequestMapping("foo")
56+
public String foo() {
57+
throw new RuntimeException("Expected exception in controller");
58+
}
59+
60+
@GetMapping(value = "delete/{id}")
61+
public ModelAndView delete(@PathVariable("id") Long id) {
62+
this.messageRepository.deleteMessage(id);
63+
Iterable<Message> messages = this.messageRepository.findAll();
64+
return new ModelAndView("messages/list", "messages", messages);
65+
}
66+
67+
@GetMapping(value = "modify/{id}")
68+
public ModelAndView modifyForm(@PathVariable("id") Message message) {
69+
return new ModelAndView("messages/form", "message", message);
70+
}
71+
72+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.neo.model;
2+
3+
import java.util.Calendar;
4+
5+
import javax.validation.constraints.NotEmpty;
6+
7+
8+
public class Message {
9+
10+
private Long id;
11+
12+
@NotEmpty(message = "Text is required.")
13+
private String text;
14+
15+
@NotEmpty(message = "Summary is required.")
16+
private String summary;
17+
18+
private Calendar created = Calendar.getInstance();
19+
20+
public Long getId() {
21+
return this.id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public Calendar getCreated() {
29+
return this.created;
30+
}
31+
32+
public void setCreated(Calendar created) {
33+
this.created = created;
34+
}
35+
36+
public String getText() {
37+
return this.text;
38+
}
39+
40+
public void setText(String text) {
41+
this.text = text;
42+
}
43+
44+
public String getSummary() {
45+
return this.summary;
46+
}
47+
48+
public void setSummary(String summary) {
49+
this.summary = summary;
50+
}
51+
52+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
package com.neo.repository;
4+
5+
import com.neo.model.Message;
6+
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ConcurrentMap;
9+
import java.util.concurrent.atomic.AtomicLong;
10+
11+
12+
public class InMemoryMessageRepository implements MessageRepository {
13+
14+
private static AtomicLong counter = new AtomicLong();
15+
16+
private final ConcurrentMap<Long, Message> messages = new ConcurrentHashMap<>();
17+
18+
@Override
19+
public Iterable<Message> findAll() {
20+
return this.messages.values();
21+
}
22+
23+
@Override
24+
public Message save(Message message) {
25+
Long id = message.getId();
26+
if (id == null) {
27+
id = counter.incrementAndGet();
28+
message.setId(id);
29+
}
30+
this.messages.put(id, message);
31+
return message;
32+
}
33+
34+
@Override
35+
public Message findMessage(Long id) {
36+
return this.messages.get(id);
37+
}
38+
39+
@Override
40+
public void deleteMessage(Long id) {
41+
this.messages.remove(id);
42+
}
43+
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
package com.neo.repository;
3+
4+
import com.neo.model.Message;
5+
6+
public interface MessageRepository {
7+
8+
Iterable<Message> findAll();
9+
10+
Message save(Message message);
11+
12+
Message findMessage(Long id);
13+
14+
void deleteMessage(Long id);
15+
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Allow Thymeleaf templates to be reloaded at dev time
2+
spring.thymeleaf.cache: false
3+
server.tomcat.access_log_enabled: true
4+
server.tomcat.basedir: target/tomcat
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
5+
6+
<!-- logger name="org.springframework" level="DEBUG"/-->
7+
8+
</configuration>

spring-boot-web-thymeleaf/src/main/resources/static/css/bootstrap.min.css

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head th:fragment="head (title)">
4+
<title th:text="${title}">Fragments</title>
5+
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
6+
href="../../css/bootstrap.min.css" />
7+
</head>
8+
<body>
9+
<div class="container">
10+
<nav th:fragment="navbar" class="navbar navbar-dark bg-primary">
11+
<a class="navbar-brand" href="http://www.ityouknow.com">Ityouknow</a>
12+
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
13+
<li class="nav-item"><a class="nav-link" th:href="@{/}" href="messages.html">Messages</a></li>
14+
</ul>
15+
</nav>
16+
</div>
17+
</body>
18+
</html>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head th:replace="fragments :: head(title=~{::title/text()})">
4+
<title>Messages : Create</title>
5+
</head>
6+
<body>
7+
<div class="container">
8+
<div th:replace="fragments :: navbar"></div>
9+
<div class="float-right mt-2">
10+
<a class="btn btn-primary btn-sm" th:href="@{/}" href="messages.html"> Messages </a>
11+
</div>
12+
<h4 class="float-left mt-2">Messages : Create</h4>
13+
<div class="clearfix"></div>
14+
<form id="messageForm" th:action="@{/(form)}" th:object="${message}" action="#" method="post">
15+
<div th:if="${#fields.hasErrors('*')}" class="alert alert-danger" role="alert">
16+
<p th:each="error : ${#fields.errors('*')}" class="m-0" th:text="${error}">Validation error</p>
17+
</div>
18+
<input type="hidden" th:field="*{id}" th:class="${'form-control' + (#fields.hasErrors('id') ? ' is-invalid' : '')}"/>
19+
<div class="form-group">
20+
<label for="summary">Summary</label>
21+
<input type="text" th:field="*{summary}" th:class="${'form-control' + (#fields.hasErrors('summary') ? ' is-invalid' : '')}">
22+
</div>
23+
<div class="form-group">
24+
<label for="text">Message</label>
25+
<textarea th:field="*{text}" th:class="${'form-control' + (#fields.hasErrors('text') ? ' is-invalid' : '')}"></textarea>
26+
</div>
27+
<button type="submit" class="btn btn-primary">Submit</button>
28+
</form>
29+
</div>
30+
</body>
31+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head th:replace="fragments :: head(title=~{::title/text()})">
4+
<title>Messages : View all</title>
5+
</head>
6+
<body>
7+
<div class="container">
8+
<div th:replace="fragments :: navbar"></div>
9+
<div class="float-right mt-2">
10+
<a class="btn btn-primary btn-sm" href="form.html" th:href="@{/(form)}">Create Message</a>
11+
</div>
12+
<h4 class="float-left mt-2">Messages : View all</h4>
13+
<table class="table table-bordered table-striped">
14+
<thead>
15+
<tr>
16+
<th>ID</th>
17+
<th>Created</th>
18+
<th>Summary</th>
19+
</tr>
20+
</thead>
21+
<tbody>
22+
<tr th:if="${messages.empty}">
23+
<td colspan="3">No messages</td>
24+
</tr>
25+
<tr th:each="message : ${messages}">
26+
<td th:text="${message.id}">1</td>
27+
<td th:text="${#calendars.format(message.created)}">July 11,
28+
2012 2:17:16 PM CDT</td>
29+
<td><a href="view.html" th:href="@{'/' + ${message.id}}"
30+
th:text="${message.summary}"> The summary </a></td>
31+
</tr>
32+
</tbody>
33+
</table>
34+
</div>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)