Skip to content

Commit b1c4b3d

Browse files
committed
REDIRECT AND FLASH ATTRIBUTE RELATED IMPROVEMENTS
Remove custom FlashMap extension. Update FormController to use Spring 3.1 flash attributes. Use @ModelAttribute method to pre-load "ajaxRequest" attribute. USe @SessionAttribute to store "formBean" attribute in the session. Add "Redirect" tab showing options for building the redirect URL.
1 parent e5b82d0 commit b1c4b3d

File tree

16 files changed

+141
-176
lines changed

16 files changed

+141
-176
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>1.0.0-BUILD-SNAPSHOT</version>
1010
<properties>
1111
<java-version>1.6</java-version>
12-
<org.springframework-version>3.1.0.M2</org.springframework-version>
12+
<org.springframework-version>3.1.0.BUILD-SNAPSHOT</org.springframework-version>
1313
<org.aspectj-version>1.6.10</org.aspectj-version>
1414
<org.slf4j-version>1.6.1</org.slf4j-version>
1515
</properties>

src/main/java/org/springframework/mvc/extensions/flash/FlashMap.java

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/main/java/org/springframework/mvc/extensions/flash/FlashMapFilter.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/main/java/org/springframework/samples/mvc/fileupload/FileUploadController.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import java.io.IOException;
44

55
import org.springframework.mvc.extensions.ajax.AjaxUtils;
6-
import org.springframework.mvc.extensions.flash.FlashMap.Message;
7-
import org.springframework.mvc.extensions.flash.FlashMap.MessageType;
86
import org.springframework.stereotype.Controller;
97
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.ModelAttribute;
109
import org.springframework.web.bind.annotation.RequestMapping;
1110
import org.springframework.web.bind.annotation.RequestMethod;
1211
import org.springframework.web.bind.annotation.RequestParam;
@@ -17,17 +16,18 @@
1716
@RequestMapping("/fileupload")
1817
public class FileUploadController {
1918

19+
@ModelAttribute
20+
public void ajaxAttribute(WebRequest request, Model model) {
21+
model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
22+
}
23+
2024
@RequestMapping(method=RequestMethod.GET)
21-
public void fileUploadForm(WebRequest webRequest, Model model) {
22-
model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(webRequest));
25+
public void fileUploadForm() {
2326
}
2427

2528
@RequestMapping(method=RequestMethod.POST)
26-
public void processUpload(@RequestParam MultipartFile file, WebRequest webRequest, Model model) throws IOException {
27-
String message = "File '" + file.getOriginalFilename() + "' uploaded successfully";
28-
// prepare model for rendering success message in this request
29-
model.addAttribute("message", new Message(MessageType.success, message));
30-
model.addAttribute("ajaxRequest", AjaxUtils.isAjaxUploadRequest(webRequest));
29+
public void processUpload(@RequestParam MultipartFile file, Model model) throws IOException {
30+
model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
3131
}
3232

3333
}

src/main/java/org/springframework/samples/mvc/form/FormController.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,60 @@
11
package org.springframework.samples.mvc.form;
22

3-
import javax.servlet.http.HttpSession;
43
import javax.validation.Valid;
54

65
import org.springframework.mvc.extensions.ajax.AjaxUtils;
7-
import org.springframework.mvc.extensions.flash.FlashMap;
8-
import org.springframework.mvc.extensions.flash.FlashMap.Message;
9-
import org.springframework.mvc.extensions.flash.FlashMap.MessageType;
106
import org.springframework.stereotype.Controller;
117
import org.springframework.ui.Model;
128
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.ModelAttribute;
1310
import org.springframework.web.bind.annotation.RequestMapping;
1411
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.SessionAttributes;
1513
import org.springframework.web.context.request.WebRequest;
14+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
1615

1716
@Controller
1817
@RequestMapping("/form")
18+
@SessionAttributes("formBean")
1919
public class FormController {
2020

21+
// Invoked on every request
22+
23+
@ModelAttribute
24+
public void ajaxAttribute(WebRequest request, Model model) {
25+
model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));
26+
}
27+
28+
// Invoked initially to create the "form" attribute
29+
// Once created the "form" attribute comes from the HTTP session (see @SessionAttributes)
30+
31+
@ModelAttribute("formBean")
32+
public FormBean createFormBean() {
33+
return new FormBean();
34+
}
35+
2136
@RequestMapping(method=RequestMethod.GET)
22-
public void form(WebRequest webRequest, HttpSession session, Model model) {
23-
FormBean form = (FormBean) session.getAttribute("form");
24-
model.addAttribute(form != null ? form : new FormBean());
25-
model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(webRequest));
37+
public void form() {
2638
}
2739

2840
@RequestMapping(method=RequestMethod.POST)
29-
public String processSubmit(@Valid FormBean form, BindingResult result, WebRequest webRequest, HttpSession session, Model model) {
41+
public String processSubmit(@Valid FormBean formBean, BindingResult result, boolean ajaxRequest,
42+
Model model, RedirectAttributes redirectAttrs) {
3043
if (result.hasErrors()) {
31-
model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(webRequest));
3244
return null;
3345
}
34-
// simply store form bean in the session for demo purposes, typically you would save form bean values to a db
35-
session.setAttribute("form", form);
36-
String message = "Form submitted successfully. Bound " + form;
37-
// success response handling
38-
if (AjaxUtils.isAjaxRequest(webRequest)) {
46+
// Typically you would save to a db and clear the "form" attribute from the session
47+
// via SessionStatus.setCompleted(). For the demo we leave it in the session.
48+
String message = "Form submitted successfully. Bound " + formBean;
49+
// Success response handling
50+
if (ajaxRequest) {
3951
// prepare model for rendering success message in this request
40-
model.addAttribute("message", new Message(MessageType.success, message));
41-
model.addAttribute("ajaxRequest", true);
52+
model.addAttribute("message", message);
4253
return null;
4354
} else {
4455
// store a success message for rendering on the next request after redirect
45-
FlashMap.setSuccessMessage(message);
4656
// redirect back to the form to render the success message along with newly bound values
57+
redirectAttrs.addFlashAttribute("message", message);
4758
return "redirect:/form";
4859
}
4960
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.springframework.samples.mvc.redirect;
2+
3+
import javax.inject.Inject;
4+
5+
import org.joda.time.LocalDate;
6+
import org.springframework.core.convert.ConversionService;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
13+
import org.springframework.web.util.UriComponents;
14+
import org.springframework.web.util.UriComponentsBuilder;
15+
16+
@Controller
17+
@RequestMapping("/redirect")
18+
public class RedirectController {
19+
20+
private final ConversionService conversionService;
21+
22+
@Inject
23+
public RedirectController(ConversionService conversionService) {
24+
this.conversionService = conversionService;
25+
}
26+
27+
@RequestMapping(value="/uriTemplate", method=RequestMethod.GET)
28+
public String uriTemplate(RedirectAttributes redirectAttrs) {
29+
redirectAttrs.addAttribute("account", "a123"); // Used as URI template variable
30+
redirectAttrs.addAttribute("date", new LocalDate(2011, 12, 31)); // Appended as a query parameter
31+
return "redirect:/redirect/{account}";
32+
}
33+
34+
@RequestMapping(value="/uriComponentsBuilder", method=RequestMethod.GET)
35+
public String uriComponentsBuilder() {
36+
String date = this.conversionService.convert(new LocalDate(2011, 12, 31), String.class);
37+
UriComponents redirectUri = UriComponentsBuilder.fromPath("/redirect/{account}").queryParam("date", date)
38+
.build().expand("a123").encode();
39+
return "redirect:" + redirectUri.toUriString();
40+
}
41+
42+
@RequestMapping(value="/{account}", method=RequestMethod.GET)
43+
public String show(@PathVariable String account, @RequestParam(required=false) LocalDate date) {
44+
return "redirect/redirectResults";
45+
}
46+
47+
}

src/main/java/org/springframework/samples/mvc/views/ViewsController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ public void usingRequestToViewNameTranslator(Model model) {
2525
model.addAttribute("fruit", "apple");
2626
}
2727

28-
@RequestMapping(value="pathVars/{foo}/{fruit}", method=RequestMethod.GET)
28+
@RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)
2929
public String pathVars(@PathVariable String foo, @PathVariable String fruit) {
30+
// No need to add @PathVariables "foo" and "fruit" to the model
31+
// They will be merged in the model before rendering
3032
return "views/html";
3133
}
3234

3335
@RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)
3436
public String dataBinding(@Valid JavaBean javaBean, Model model) {
37+
// JavaBean "foo" and "fruit" properties populated from URI variables
3538
return "views/dataBinding";
3639
}
3740

src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package -->
2828
<beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
2929
<beans:property name="formatters">
30-
<beans:bean class="org.springframework.samples.mvc.convert.MaskFormatAnnotationFormatterFactory" />
30+
<beans:bean class="org.springframework.samples.mvc.convert.MaskFormatAnnotationFormatterFactory" />
3131
</beans:property>
3232
</beans:bean>
3333

src/main/webapp/WEB-INF/views/fileupload.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<div class="header">
1919
<h2>Form</h2>
2020
<c:if test="${not empty message}">
21-
<div id="message" class="${message.type}">${message.text}</div>
21+
<div id="message" class="success">${message}</div>
2222
</c:if>
2323
</div>
2424
<label for="file">File</label>

src/main/webapp/WEB-INF/views/form.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<div class="header">
2121
<h2>Form</h2>
2222
<c:if test="${not empty message}">
23-
<div id="message" class="${message.type}">${message.text}</div>
23+
<div id="message" class="success">${message}</div>
2424
</c:if>
2525
<s:bind path="*">
2626
<c:if test="${status.error}">

0 commit comments

Comments
 (0)