EN
Java - spring attribute redirection
13 points
In Spring Freamwork it is possible to make attribute redirection in following way.
RedirectionExampleController.java
file:
xxxxxxxxxx
1
package com.dirask.examples;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.ui.Model;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.RequestMethod;
7
import org.springframework.web.bind.annotation.ResponseBody;
8
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
9
10
import java.util.Map;
11
12
13
public class RedirectionExampleController {
14
15
value = "/redirect-to-my-final-page", (
16
method = RequestMethod.GET)
17
public String redirectToMyFinalPage(RedirectAttributes attributes) {
18
19
attributes.addFlashAttribute("my-redirected-message",
20
"This is my message!");
21
22
return "redirect:/my-final-page";
23
}
24
25
value = "/my-final-page", (
26
method = RequestMethod.GET)
27
28
public String myFinalPage(Model model) {
29
30
Map<String, Object> attributes = model.asMap();
31
32
return (String)attributes.get("my-redirected-message");
33
}
34
}
Running:
- Run server, e.g. on
localhost:8080
- Open in browser following link:
http://localhost:8080/redirect-to-my-final-page
Result:
Note: web page should be redirected to
http://localhost:8080/my-final-page
.
In this section alternative redirection approach with ModelAndView
class is presented.
RedirectionExampleController.java
file:
xxxxxxxxxx
1
package com.dirask.examples;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.ui.Model;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.RequestMethod;
7
import org.springframework.web.bind.annotation.ResponseBody;
8
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
9
10
import java.util.Map;
11
12
13
public class RedirectionExampleController {
14
15
value = "/redirect-to-my-final-page", (
16
method = RequestMethod.GET)
17
public ModelAndView redirectToMyFinalPage(RedirectAttributes attributes) {
18
19
attributes.addFlashAttribute("my-redirected-message",
20
"This is my message!");
21
22
return new ModelAndView(new RedirectView("/my-final-page", true));
23
}
24
25
value = "/my-final-page", (
26
method = RequestMethod.GET)
27
28
public String myFinalPage(Model model) {
29
30
Map<String, Object> attributes = model.asMap();
31
32
return (String)attributes.get("my-redirected-message");
33
}
34
}
In this section alternative redirection approach with RedirectView
class is presented.
RedirectionExampleController.java
file:
xxxxxxxxxx
1
package com.dirask.examples;
2
3
import org.springframework.stereotype.Controller;
4
import org.springframework.ui.Model;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.RequestMethod;
7
import org.springframework.web.bind.annotation.ResponseBody;
8
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
9
10
import java.util.Map;
11
12
13
public class RedirectionExampleController {
14
15
value = "/redirect-to-my-final-page", (
16
method = RequestMethod.GET)
17
public RedirectView redirectToMyFinalPage(RedirectAttributes attributes) {
18
19
attributes.addFlashAttribute("my-redirected-message",
20
"This is my message!");
21
22
return new RedirectView("/my-final-page", true);
23
}
24
25
value = "/my-final-page", (
26
method = RequestMethod.GET)
27
28
public String myFinalPage(Model model) {
29
30
Map<String, Object> attributes = model.asMap();
31
32
return (String)attributes.get("my-redirected-message");
33
}
34
}