Skip to content

Commit 463b97f

Browse files
tapankavasthiKevinGilmore
authored andcommitted
BAEL-3284: Guide to Flash Attributes in a Spring Web Application (eugenp#8070)
1 parent 563f74a commit 463b97f

File tree

6 files changed

+174
-1
lines changed

6 files changed

+174
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.flash_attributes;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
6+
7+
@SpringBootApplication
8+
public class Application extends SpringBootServletInitializer {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
14+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.flash_attributes.controllers;
2+
3+
import com.baeldung.flash_attributes.model.Poem;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.ModelAttribute;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
10+
import org.springframework.web.servlet.support.RequestContextUtils;
11+
import org.springframework.web.servlet.view.RedirectView;
12+
import java.util.Map;
13+
14+
import javax.servlet.http.HttpServletRequest;
15+
16+
@Controller
17+
public class PoemSubmission {
18+
19+
@GetMapping("/poem/success")
20+
public String getSuccess(HttpServletRequest request) {
21+
Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);
22+
if (inputFlashMap != null) {
23+
Poem poem = (Poem) inputFlashMap.get("poem");
24+
return "success";
25+
} else {
26+
return "redirect:/poem/submit";
27+
}
28+
}
29+
30+
@PostMapping("/poem/submit")
31+
public RedirectView submitPost(
32+
HttpServletRequest request,
33+
@ModelAttribute Poem poem,
34+
RedirectAttributes redirectAttributes) {
35+
if (Poem.isValidPoem(poem)) {
36+
redirectAttributes.addFlashAttribute("poem", poem);
37+
return new RedirectView("/poem/success", true);
38+
} else {
39+
return new RedirectView("/poem/submit", true);
40+
}
41+
}
42+
43+
@GetMapping("/poem/submit")
44+
public String submitGet(Model model) {
45+
model.addAttribute("poem", new Poem());
46+
return "submit";
47+
}
48+
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.flash_attributes.model;
2+
3+
import org.apache.logging.log4j.util.Strings;
4+
5+
public class Poem {
6+
private String title;
7+
private String author;
8+
private String body;
9+
10+
public static boolean isValidPoem(Poem poem) {
11+
return poem != null && Strings.isNotBlank(poem.getAuthor()) && Strings.isNotBlank(poem.getBody())
12+
&& Strings.isNotBlank(poem.getTitle());
13+
}
14+
15+
public String getTitle() {
16+
return this.title;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
23+
public String getBody() {
24+
return this.body;
25+
}
26+
27+
public void setBody(String body) {
28+
this.body = body;
29+
}
30+
31+
public String getAuthor() {
32+
return this.author;
33+
}
34+
35+
public void setAuthor(String author) {
36+
this.author = author;
37+
}
38+
39+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
spring.main.allow-bean-definition-overriding=true
2+
23
spring.mail.host=localhost
3-
spring.mail.port=8025
4+
spring.mail.port=8025
5+
6+
spring.thymeleaf.cache=false
7+
spring.thymeleaf.enabled=true
8+
spring.thymeleaf.prefix=classpath:/templates/
9+
spring.thymeleaf.suffix=.html
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Poetry Contest: Submission</title>
5+
</head>
6+
<body>
7+
8+
<form action="#" method="post" th:action="@{/poem/submit}" th:object="${poem}">
9+
10+
<table border='0' cellpadding='2' cellspacing='2' width='480px'>
11+
12+
<tr>
13+
<td align='center'>
14+
<label for="author">Author:</label>
15+
</td>
16+
<td>
17+
<input th:field="*{author}" type="text"/>
18+
</td>
19+
</tr>
20+
21+
<tr>
22+
<td align='center'>
23+
<label for="title">Poem Title:</label>
24+
</td>
25+
<td>
26+
<input th:field="*{title}" type="text"/>
27+
</td>
28+
</tr>
29+
30+
<tr>
31+
<td align='center'>
32+
<label for="body">Poem:</label>
33+
</td>
34+
<td>
35+
<textarea cols="30" rows="30" th:field="*{body}"/>
36+
</td>
37+
</tr>
38+
39+
<tr>
40+
<td align='center'>
41+
<input type="submit" value="Submit Poem">
42+
</td>
43+
</tr>
44+
45+
46+
</table>
47+
</form>
48+
49+
</body>
50+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Poetry Contest: Thank You</title>
5+
</head>
6+
<body>
7+
<h1 th:if="${poem}">
8+
<p th:text="${'You have successfully submitted poem titled - '+ poem?.title}"/>
9+
Click <a th:href="@{/poem/submit}"> here</a> to submit more.
10+
</h1>
11+
<h1 th:unless="${poem}">
12+
Click <a th:href="@{/poem/submit}"> here</a> to submit a poem.
13+
</h1>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)