forked from ityouknow/spring-boot-examples
-
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.
- Loading branch information
Showing
18 changed files
with
541 additions
and
2 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
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
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,40 @@ | ||
<?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> | ||
<artifactId>spring-boot-web-thymeleaf</artifactId> | ||
<name>Spring Boot Web thymeleaf Sample</name> | ||
<description>Spring Boot Web thymeleaf Sample</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.0.0.RELEASE</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<!-- Compile --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | ||
</dependency> | ||
<!-- Test --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
35 changes: 35 additions & 0 deletions
35
spring-boot-web-thymeleaf/src/main/java/com/neo/ThymeleafApplication.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,35 @@ | ||
|
||
|
||
package com.neo; | ||
|
||
import com.neo.model.Message; | ||
import com.neo.repository.InMemoryMessageRepository; | ||
import com.neo.repository.MessageRepository; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.convert.converter.Converter; | ||
|
||
@SpringBootApplication | ||
public class ThymeleafApplication { | ||
|
||
@Bean | ||
public MessageRepository messageRepository() { | ||
return new InMemoryMessageRepository(); | ||
} | ||
|
||
@Bean | ||
public Converter<String, Message> messageConverter() { | ||
return new Converter<String, Message>() { | ||
@Override | ||
public Message convert(String id) { | ||
return messageRepository().findMessage(Long.valueOf(id)); | ||
} | ||
}; | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ThymeleafApplication.class, args); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
spring-boot-web-thymeleaf/src/main/java/com/neo/controller/MessageController.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,72 @@ | ||
|
||
package com.neo.controller; | ||
|
||
import javax.validation.Valid; | ||
|
||
import com.neo.model.Message; | ||
import com.neo.repository.MessageRepository; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import org.springframework.web.servlet.mvc.support.RedirectAttributes; | ||
|
||
@Controller | ||
@RequestMapping("/") | ||
public class MessageController { | ||
|
||
private final MessageRepository messageRepository; | ||
|
||
public MessageController(MessageRepository messageRepository) { | ||
this.messageRepository = messageRepository; | ||
} | ||
|
||
@GetMapping | ||
public ModelAndView list() { | ||
Iterable<Message> messages = this.messageRepository.findAll(); | ||
return new ModelAndView("messages/list", "messages", messages); | ||
} | ||
|
||
@GetMapping("{id}") | ||
public ModelAndView view(@PathVariable("id") Message message) { | ||
return new ModelAndView("messages/view", "message", message); | ||
} | ||
|
||
@GetMapping(params = "form") | ||
public String createForm(@ModelAttribute Message message) { | ||
return "messages/form"; | ||
} | ||
|
||
@PostMapping | ||
public ModelAndView create(@Valid Message message, BindingResult result, | ||
RedirectAttributes redirect) { | ||
if (result.hasErrors()) { | ||
return new ModelAndView("messages/form", "formErrors", result.getAllErrors()); | ||
} | ||
message = this.messageRepository.save(message); | ||
redirect.addFlashAttribute("globalMessage", "Successfully created a new message"); | ||
return new ModelAndView("redirect:/{message.id}", "message.id", message.getId()); | ||
} | ||
|
||
@RequestMapping("foo") | ||
public String foo() { | ||
throw new RuntimeException("Expected exception in controller"); | ||
} | ||
|
||
@GetMapping(value = "delete/{id}") | ||
public ModelAndView delete(@PathVariable("id") Long id) { | ||
this.messageRepository.deleteMessage(id); | ||
Iterable<Message> messages = this.messageRepository.findAll(); | ||
return new ModelAndView("messages/list", "messages", messages); | ||
} | ||
|
||
@GetMapping(value = "modify/{id}") | ||
public ModelAndView modifyForm(@PathVariable("id") Message message) { | ||
return new ModelAndView("messages/form", "message", message); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
spring-boot-web-thymeleaf/src/main/java/com/neo/model/Message.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,52 @@ | ||
package com.neo.model; | ||
|
||
import java.util.Calendar; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
|
||
public class Message { | ||
|
||
private Long id; | ||
|
||
@NotEmpty(message = "Text is required.") | ||
private String text; | ||
|
||
@NotEmpty(message = "Summary is required.") | ||
private String summary; | ||
|
||
private Calendar created = Calendar.getInstance(); | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public Calendar getCreated() { | ||
return this.created; | ||
} | ||
|
||
public void setCreated(Calendar created) { | ||
this.created = created; | ||
} | ||
|
||
public String getText() { | ||
return this.text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
public String getSummary() { | ||
return this.summary; | ||
} | ||
|
||
public void setSummary(String summary) { | ||
this.summary = summary; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
spring-boot-web-thymeleaf/src/main/java/com/neo/repository/InMemoryMessageRepository.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,44 @@ | ||
|
||
|
||
package com.neo.repository; | ||
|
||
import com.neo.model.Message; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
|
||
public class InMemoryMessageRepository implements MessageRepository { | ||
|
||
private static AtomicLong counter = new AtomicLong(); | ||
|
||
private final ConcurrentMap<Long, Message> messages = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public Iterable<Message> findAll() { | ||
return this.messages.values(); | ||
} | ||
|
||
@Override | ||
public Message save(Message message) { | ||
Long id = message.getId(); | ||
if (id == null) { | ||
id = counter.incrementAndGet(); | ||
message.setId(id); | ||
} | ||
this.messages.put(id, message); | ||
return message; | ||
} | ||
|
||
@Override | ||
public Message findMessage(Long id) { | ||
return this.messages.get(id); | ||
} | ||
|
||
@Override | ||
public void deleteMessage(Long id) { | ||
this.messages.remove(id); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-web-thymeleaf/src/main/java/com/neo/repository/MessageRepository.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,16 @@ | ||
|
||
package com.neo.repository; | ||
|
||
import com.neo.model.Message; | ||
|
||
public interface MessageRepository { | ||
|
||
Iterable<Message> findAll(); | ||
|
||
Message save(Message message); | ||
|
||
Message findMessage(Long id); | ||
|
||
void deleteMessage(Long id); | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
spring-boot-web-thymeleaf/src/main/resources/application.properties
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,4 @@ | ||
# Allow Thymeleaf templates to be reloaded at dev time | ||
spring.thymeleaf.cache: false | ||
server.tomcat.access_log_enabled: true | ||
server.tomcat.basedir: target/tomcat |
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
|
||
<include resource="org/springframework/boot/logging/logback/base.xml"/> | ||
|
||
<!-- logger name="org.springframework" level="DEBUG"/--> | ||
|
||
</configuration> |
7 changes: 7 additions & 0 deletions
7
spring-boot-web-thymeleaf/src/main/resources/static/css/bootstrap.min.css
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
18 changes: 18 additions & 0 deletions
18
spring-boot-web-thymeleaf/src/main/resources/templates/fragments.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,18 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head th:fragment="head (title)"> | ||
<title th:text="${title}">Fragments</title> | ||
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" | ||
href="../../css/bootstrap.min.css" /> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<nav th:fragment="navbar" class="navbar navbar-dark bg-primary"> | ||
<a class="navbar-brand" href="http://www.ityouknow.com">Ityouknow</a> | ||
<ul class="navbar-nav mr-auto mt-2 mt-lg-0"> | ||
<li class="nav-item"><a class="nav-link" th:href="@{/}" href="messages.html">Messages</a></li> | ||
</ul> | ||
</nav> | ||
</div> | ||
</body> | ||
</html> |
31 changes: 31 additions & 0 deletions
31
spring-boot-web-thymeleaf/src/main/resources/templates/messages/form.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,31 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head th:replace="fragments :: head(title=~{::title/text()})"> | ||
<title>Messages : Create</title> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div th:replace="fragments :: navbar"></div> | ||
<div class="float-right mt-2"> | ||
<a class="btn btn-primary btn-sm" th:href="@{/}" href="messages.html"> Messages </a> | ||
</div> | ||
<h4 class="float-left mt-2">Messages : Create</h4> | ||
<div class="clearfix"></div> | ||
<form id="messageForm" th:action="@{/(form)}" th:object="${message}" action="#" method="post"> | ||
<div th:if="${#fields.hasErrors('*')}" class="alert alert-danger" role="alert"> | ||
<p th:each="error : ${#fields.errors('*')}" class="m-0" th:text="${error}">Validation error</p> | ||
</div> | ||
<input type="hidden" th:field="*{id}" th:class="${'form-control' + (#fields.hasErrors('id') ? ' is-invalid' : '')}"/> | ||
<div class="form-group"> | ||
<label for="summary">Summary</label> | ||
<input type="text" th:field="*{summary}" th:class="${'form-control' + (#fields.hasErrors('summary') ? ' is-invalid' : '')}"> | ||
</div> | ||
<div class="form-group"> | ||
<label for="text">Message</label> | ||
<textarea th:field="*{text}" th:class="${'form-control' + (#fields.hasErrors('text') ? ' is-invalid' : '')}"></textarea> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</form> | ||
</div> | ||
</body> | ||
</html> |
36 changes: 36 additions & 0 deletions
36
spring-boot-web-thymeleaf/src/main/resources/templates/messages/list.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,36 @@ | ||
<!DOCTYPE html> | ||
<html xmlns:th="http://www.thymeleaf.org"> | ||
<head th:replace="fragments :: head(title=~{::title/text()})"> | ||
<title>Messages : View all</title> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div th:replace="fragments :: navbar"></div> | ||
<div class="float-right mt-2"> | ||
<a class="btn btn-primary btn-sm" href="form.html" th:href="@{/(form)}">Create Message</a> | ||
</div> | ||
<h4 class="float-left mt-2">Messages : View all</h4> | ||
<table class="table table-bordered table-striped"> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>Created</th> | ||
<th>Summary</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr th:if="${messages.empty}"> | ||
<td colspan="3">No messages</td> | ||
</tr> | ||
<tr th:each="message : ${messages}"> | ||
<td th:text="${message.id}">1</td> | ||
<td th:text="${#calendars.format(message.created)}">July 11, | ||
2012 2:17:16 PM CDT</td> | ||
<td><a href="view.html" th:href="@{'/' + ${message.id}}" | ||
th:text="${message.summary}"> The summary </a></td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
</body> | ||
</html> |
Oops, something went wrong.