diff --git a/pom.xml b/pom.xml index c939ddb..5a0d73b 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.springframework.boot - spring-boot-starter-mustache + spring-boot-starter-freemarker org.springframework.boot diff --git a/src/main/java/net/sytes/kashey/sweater/controller/MainController.java b/src/main/java/net/sytes/kashey/sweater/controller/MainController.java index 5f58f3a..b2d3745 100644 --- a/src/main/java/net/sytes/kashey/sweater/controller/MainController.java +++ b/src/main/java/net/sytes/kashey/sweater/controller/MainController.java @@ -8,12 +8,11 @@ import net.sytes.kashey.sweater.security.CustomUserDetails; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; -import java.util.Map; - @Controller public class MainController { @@ -31,9 +30,14 @@ public class MainController { } @GetMapping("/main") - public String findAllMessages(Map model) { + public String findAllMessages(@RequestParam(required = false, defaultValue = "") String tag, Model model) { - model.put("messages", messageRepository.findAll()); + if (tag == null || tag.isEmpty()) { + model.addAttribute("messages", messageRepository.findAll()); + } else { + model.addAttribute("messages", messageRepository.findByTag(tag)); + } + model.addAttribute("tag", tag); return "main"; } @@ -41,24 +45,15 @@ public class MainController { public String addMessage(@AuthenticationPrincipal CustomUserDetails details, @RequestParam String text, @RequestParam String tag, - Map model) { + Model model) { User author = userRepository.findByUsername(details.getUsername()) .orElseThrow(() -> new UsernameNotFoundException("User not found: " + details.getUsername())); messageRepository.save(new Message(text, tag, author)); - model.put("messages", messageRepository.findAll()); + model.addAttribute("messages", messageRepository.findAll()); + model.addAttribute("tag", ""); return "main"; } - @PostMapping("filter") - public String filterByTag(@RequestParam String tag, Map model) { - - if (tag == null || tag.isEmpty()) { - model.put("messages", messageRepository.findAll()); - } else { - model.put("messages", messageRepository.findByTag(tag)); - } - return "main"; - } } \ No newline at end of file diff --git a/src/main/java/net/sytes/kashey/sweater/domain/Message.java b/src/main/java/net/sytes/kashey/sweater/domain/Message.java index 6c5f74e..b7c2fcf 100644 --- a/src/main/java/net/sytes/kashey/sweater/domain/Message.java +++ b/src/main/java/net/sytes/kashey/sweater/domain/Message.java @@ -5,7 +5,7 @@ import jakarta.persistence.*; @Entity public class Message { @Id - @GeneratedValue(strategy= GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; private String text; private String tag; @@ -17,11 +17,6 @@ public class Message { public Message() { } - public Message(String text, String tag) { - this.text = text; - this.tag = tag; - } - public Message(String text, String tag, User author) { this.text = text; this.tag = tag; @@ -53,7 +48,7 @@ public class Message { } public User getAuthor() { - return author; + return author; } public void setAuthor(User author) { @@ -61,6 +56,6 @@ public class Message { } public String getAuthorName() { - return author == null ? "": author.getUsername(); + return author == null ? "<none>" : author.getUsername(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b47762d..46cda95 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,5 +3,5 @@ spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/sweater_db spring.datasource.username=user spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.mustache.servlet.expose-request-attributes=true - +spring.freemarker.expose-request-attributes=true +spring.freemarker.suffix=.ftl diff --git a/src/main/resources/templates/greeting.ftl b/src/main/resources/templates/greeting.ftl new file mode 100644 index 0000000..7cb7a07 --- /dev/null +++ b/src/main/resources/templates/greeting.ftl @@ -0,0 +1,5 @@ +<#import "parts/common.ftl" as com> +<@com.page> +
Hello there
+Main page + \ No newline at end of file diff --git a/src/main/resources/templates/greeting.mustache b/src/main/resources/templates/greeting.mustache deleted file mode 100644 index 679bd63..0000000 --- a/src/main/resources/templates/greeting.mustache +++ /dev/null @@ -1,11 +0,0 @@ - - - - Getting Started: Serving Web Content - - - -
Hello there
-Main page - - \ No newline at end of file diff --git a/src/main/resources/templates/login.ftl b/src/main/resources/templates/login.ftl new file mode 100644 index 0000000..64752ee --- /dev/null +++ b/src/main/resources/templates/login.ftl @@ -0,0 +1,8 @@ +<#import "parts/common.ftl" as com> +<#import "parts/login.ftl" as l> + +<@com.page> + Login page + <@l.login "/login" /> + Add new user + \ No newline at end of file diff --git a/src/main/resources/templates/login.mustache b/src/main/resources/templates/login.mustache deleted file mode 100644 index 2662d99..0000000 --- a/src/main/resources/templates/login.mustache +++ /dev/null @@ -1,16 +0,0 @@ - - - - Spring Security Example - - -Login page -
-
-
-
- -
-Add new user - - \ No newline at end of file diff --git a/src/main/resources/templates/main.ftl b/src/main/resources/templates/main.ftl new file mode 100644 index 0000000..f7754d3 --- /dev/null +++ b/src/main/resources/templates/main.ftl @@ -0,0 +1,36 @@ +<#import "parts/common.ftl" as com> +<#import "parts/login.ftl" as l> +<@com.page> +
+ <@l.logout /> +
+ +
+
+ + + + +
+
+
+
+ Tag to filer +
+ + +
+
+ +
Messages list
+ <#list messages as message> +
+ ${message.id} + ${message.text} + ${message.tag} + ${message.authorName} +
+ <#else> + No messages found + + \ No newline at end of file diff --git a/src/main/resources/templates/main.mustache b/src/main/resources/templates/main.mustache deleted file mode 100644 index 555f8d7..0000000 --- a/src/main/resources/templates/main.mustache +++ /dev/null @@ -1,39 +0,0 @@ - - - -
-
- - -
-
- -
-
- - - - -
-
-
-
- Tag to filer -
- - - -
-
- -
Messages list
- {{#messages}} -
- {{id}} - {{text}} - {{tag}} - {{authorName}} -
- {{/messages}} - - \ No newline at end of file diff --git a/src/main/resources/templates/parts/common.ftl b/src/main/resources/templates/parts/common.ftl new file mode 100644 index 0000000..3222d82 --- /dev/null +++ b/src/main/resources/templates/parts/common.ftl @@ -0,0 +1,14 @@ +<#macro page> + + + + Sweater + + + + + <#nested> + + + + \ No newline at end of file diff --git a/src/main/resources/templates/parts/login.ftl b/src/main/resources/templates/parts/login.ftl new file mode 100644 index 0000000..c3a6812 --- /dev/null +++ b/src/main/resources/templates/parts/login.ftl @@ -0,0 +1,15 @@ +<#macro login path> +
+
+
+
+ +
+ + +<#macro logout> +
+ + +
+ \ No newline at end of file diff --git a/src/main/resources/templates/registration.ftl b/src/main/resources/templates/registration.ftl new file mode 100644 index 0000000..6b6b6e7 --- /dev/null +++ b/src/main/resources/templates/registration.ftl @@ -0,0 +1,11 @@ +<#import "parts/common.ftl" as com> +<#import "parts/login.ftl" as l> + +<@com.page> + Registration page +
+ <#if message??> + ${message} + + <@l.login "/registration" /> + diff --git a/src/main/resources/templates/registration.mustache b/src/main/resources/templates/registration.mustache deleted file mode 100644 index 3c2ecc4..0000000 --- a/src/main/resources/templates/registration.mustache +++ /dev/null @@ -1,20 +0,0 @@ - - - - Spring Security Example - - -Registration page -
-{{#message}} - {{message}} -{{/message}} -
-
-
-
- -
- - - \ No newline at end of file