|
11 | 11 | import static org.springframework.http.MediaType.IMAGE_PNG_VALUE;
|
12 | 12 | import org.springframework.http.ResponseEntity;
|
13 | 13 | import org.springframework.stereotype.Controller;
|
| 14 | +import org.springframework.ui.ModelMap; |
14 | 15 | import org.springframework.util.FileCopyUtils;
|
15 | 16 | import org.springframework.web.bind.annotation.ModelAttribute;
|
16 | 17 | import org.springframework.web.bind.annotation.PathVariable;
|
17 | 18 | import org.springframework.web.bind.annotation.RequestBody;
|
18 | 19 | import org.springframework.web.bind.annotation.RequestMapping;
|
19 | 20 | import org.springframework.web.bind.annotation.RequestMethod;
|
20 | 21 | import org.springframework.web.bind.annotation.ResponseBody;
|
| 22 | +import org.springframework.web.bind.annotation.SessionAttributes; |
| 23 | +import org.springframework.web.bind.support.SessionStatus; |
21 | 24 | import org.springframework.web.servlet.ModelAndView;
|
22 | 25 |
|
23 | 26 | import java.io.IOException;
|
|
27 | 30 | */
|
28 | 31 | @Controller
|
29 | 32 | @RequestMapping("/user")
|
| 33 | +@SessionAttributes("userSession") |
30 | 34 | public class UserController {
|
31 | 35 |
|
32 | 36 | @Autowired
|
@@ -79,4 +83,55 @@ public ResponseEntity<byte[]> showEntity(@PathVariable("imageId") String imageId
|
79 | 83 | return responseEntity;
|
80 | 84 | }
|
81 | 85 |
|
| 86 | + @RequestMapping(value = "/setId") |
| 87 | + public String setId(@ModelAttribute("user") User user) { |
| 88 | + user.setUserId("1000"); |
| 89 | + return "user/createSuccess"; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 访问UserController任何一个请求处理方法前, |
| 94 | + * Spring MVC 先执行该方法,并将返回值以 user 为键添加到模型中 |
| 95 | + */ |
| 96 | + @ModelAttribute("user") |
| 97 | + public User getUser() { |
| 98 | + User user = new User(); |
| 99 | + user.setUserId("1001"); |
| 100 | + user.setUserName("Brian"); |
| 101 | + return user; |
| 102 | + } |
| 103 | + |
| 104 | + @RequestMapping(value = "/show") |
| 105 | + public String show(ModelMap modelMap) { |
| 106 | + modelMap.addAttribute("testAttr", "testValue"); |
| 107 | + User user = (User) modelMap.get("user"); |
| 108 | + user.setUserName("Way"); |
| 109 | + return "/user/showUser"; |
| 110 | + } |
| 111 | + |
| 112 | + @RequestMapping(value = "/showRedirect") |
| 113 | + public String showRedirect(@ModelAttribute("userSession") User user) { |
| 114 | + user.setUserName("Netty"); |
| 115 | + return "redirect:/user/showSession"; |
| 116 | + } |
| 117 | + |
| 118 | + @RequestMapping(value = "/showSession") |
| 119 | + public String showSession(ModelMap modelMap, SessionStatus sessionStatus) { |
| 120 | + User user = (User) modelMap.get("userSession"); |
| 121 | + if (user != null) { |
| 122 | + user.setUserId("2001"); |
| 123 | + sessionStatus.setComplete(); |
| 124 | + }else{ |
| 125 | + System.out.println("null userSession"); |
| 126 | + } |
| 127 | + return "/user/showUser"; |
| 128 | + } |
| 129 | + |
| 130 | + @ModelAttribute("userSession") |
| 131 | + public User getUserSession() { |
| 132 | + User user = new User(); |
| 133 | + user.setUserId("2000"); |
| 134 | + user.setUserName("Apple"); |
| 135 | + return user; |
| 136 | + } |
82 | 137 | }
|
0 commit comments