BAEL-3284: Guide to Flash Attributes in a Spring Web Application (#8070)
This commit is contained in:
parent
78e23b246c
commit
940fabe5f5
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, ?> 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";
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,3 +1,9 @@
|
|||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
spring.mail.host=localhost
|
||||
spring.mail.port=8025
|
||||
|
||||
spring.thymeleaf.cache=false
|
||||
spring.thymeleaf.enabled=true
|
||||
spring.thymeleaf.prefix=classpath:/templates/
|
||||
spring.thymeleaf.suffix=.html
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Poetry Contest: Submission</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form action="#" method="post" th:action="@{/poem/submit}" th:object="${poem}">
|
||||
|
||||
<table border='0' cellpadding='2' cellspacing='2' width='480px'>
|
||||
|
||||
<tr>
|
||||
<td align='center'>
|
||||
<label for="author">Author:</label>
|
||||
</td>
|
||||
<td>
|
||||
<input th:field="*{author}" type="text"/>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td align='center'>
|
||||
<label for="title">Poem Title:</label>
|
||||
</td>
|
||||
<td>
|
||||
<input th:field="*{title}" type="text"/>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td align='center'>
|
||||
<label for="body">Poem:</label>
|
||||
</td>
|
||||
<td>
|
||||
<textarea cols="30" rows="30" th:field="*{body}"/>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td align='center'>
|
||||
<input type="submit" value="Submit Poem">
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
</table>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,15 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Poetry Contest: Thank You</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1 th:if="${poem}">
|
||||
<p th:text="${'You have successfully submitted poem titled - '+ poem?.title}"/>
|
||||
Click <a th:href="@{/poem/submit}"> here</a> to submit more.
|
||||
</h1>
|
||||
<h1 th:unless="${poem}">
|
||||
Click <a th:href="@{/poem/submit}"> here</a> to submit a poem.
|
||||
</h1>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue