From 940fabe5f5ccff6c8261d47e938da35260a80e10 Mon Sep 17 00:00:00 2001 From: Tapan Avasthi Date: Thu, 7 Nov 2019 19:06:32 +0530 Subject: [PATCH] BAEL-3284: Guide to Flash Attributes in a Spring Web Application (#8070) --- .../flash_attributes/Application.java | 14 ++++++ .../controllers/PoemSubmission.java | 49 ++++++++++++++++++ .../baeldung/flash_attributes/model/Poem.java | 39 +++++++++++++++ .../src/main/resources/application.properties | 8 ++- .../src/main/resources/templates/submit.html | 50 +++++++++++++++++++ .../src/main/resources/templates/success.html | 15 ++++++ 6 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/Application.java create mode 100644 spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/controllers/PoemSubmission.java create mode 100644 spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/model/Poem.java create mode 100644 spring-mvc-simple-2/src/main/resources/templates/submit.html create mode 100644 spring-mvc-simple-2/src/main/resources/templates/success.html diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/Application.java b/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/Application.java new file mode 100644 index 0000000000..0472ba4e7b --- /dev/null +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/Application.java @@ -0,0 +1,14 @@ +package com.baeldung.flash_attributes; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/controllers/PoemSubmission.java b/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/controllers/PoemSubmission.java new file mode 100644 index 0000000000..6264ea0531 --- /dev/null +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/controllers/PoemSubmission.java @@ -0,0 +1,49 @@ +package com.baeldung.flash_attributes.controllers; + +import com.baeldung.flash_attributes.model.Poem; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.support.RequestContextUtils; +import org.springframework.web.servlet.view.RedirectView; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +@Controller +public class PoemSubmission { + + @GetMapping("/poem/success") + public String getSuccess(HttpServletRequest request) { + Map inputFlashMap = RequestContextUtils.getInputFlashMap(request); + if (inputFlashMap != null) { + Poem poem = (Poem) inputFlashMap.get("poem"); + return "success"; + } else { + return "redirect:/poem/submit"; + } + } + + @PostMapping("/poem/submit") + public RedirectView submitPost( + HttpServletRequest request, + @ModelAttribute Poem poem, + RedirectAttributes redirectAttributes) { + if (Poem.isValidPoem(poem)) { + redirectAttributes.addFlashAttribute("poem", poem); + return new RedirectView("/poem/success", true); + } else { + return new RedirectView("/poem/submit", true); + } + } + + @GetMapping("/poem/submit") + public String submitGet(Model model) { + model.addAttribute("poem", new Poem()); + return "submit"; + } + +} \ No newline at end of file diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/model/Poem.java b/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/model/Poem.java new file mode 100644 index 0000000000..bff65456c1 --- /dev/null +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/flash_attributes/model/Poem.java @@ -0,0 +1,39 @@ +package com.baeldung.flash_attributes.model; + +import org.apache.logging.log4j.util.Strings; + +public class Poem { + private String title; + private String author; + private String body; + + public static boolean isValidPoem(Poem poem) { + return poem != null && Strings.isNotBlank(poem.getAuthor()) && Strings.isNotBlank(poem.getBody()) + && Strings.isNotBlank(poem.getTitle()); + } + + public String getTitle() { + return this.title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getBody() { + return this.body; + } + + public void setBody(String body) { + this.body = body; + } + + public String getAuthor() { + return this.author; + } + + public void setAuthor(String author) { + this.author = author; + } + +} diff --git a/spring-mvc-simple-2/src/main/resources/application.properties b/spring-mvc-simple-2/src/main/resources/application.properties index d29338d3d3..aca20f4e3c 100644 --- a/spring-mvc-simple-2/src/main/resources/application.properties +++ b/spring-mvc-simple-2/src/main/resources/application.properties @@ -1,3 +1,9 @@ spring.main.allow-bean-definition-overriding=true + spring.mail.host=localhost -spring.mail.port=8025 \ No newline at end of file +spring.mail.port=8025 + +spring.thymeleaf.cache=false +spring.thymeleaf.enabled=true +spring.thymeleaf.prefix=classpath:/templates/ +spring.thymeleaf.suffix=.html diff --git a/spring-mvc-simple-2/src/main/resources/templates/submit.html b/spring-mvc-simple-2/src/main/resources/templates/submit.html new file mode 100644 index 0000000000..0ed0107c20 --- /dev/null +++ b/spring-mvc-simple-2/src/main/resources/templates/submit.html @@ -0,0 +1,50 @@ + + + + Poetry Contest: Submission + + + +
+ + + + + + + + + + + + + + + +
+ + + +
+ + + +
+ + +