* springmvc中Model相关对象 是处理和数据相关的对象
* @ModelAttribute 重命名 参数数据
* Model(ModelMap|Map)传递数据到视图(相当于request.setAttribute)
@SessionAttributes("user")
@Controller
public class SessionController {
@ModelAttribute("user")
public User getUser() {
User user = new User();
return user;
}
/**
* http://localhost:8080/s/session?id=1
* 请求转发 forward: 不需要任何处理
*
* 请求重定向 redirect:
* 使用SessionAttribute方式 用于在重定向中传至 将值存储在session中 【用完记住清除】
* @param map
* @return
* @throws Exception
*/
@RequestMapping(value = "/session", method = RequestMethod.GET)
public String case1(@ModelAttribute("user") User user) throws Exception {
return "redirect:/session2";
}
@RequestMapping(value = "/session2", method = RequestMethod.GET)
public String case2(Map map, HttpServletResponse res,SessionStatus sessionStatus) throws Exception {
User user = (User) map.get("user");
res.getWriter().println(user.getId());
sessionStatus.setComplete();
return null;
}
}
* @ModelAttribute 重命名 参数数据
* Model(ModelMap|Map)传递数据到视图(相当于request.setAttribute)
* ModelAndView 绑定数据到视图 (ModelMap用于传递数据 View对象用于跳转)
@SessionAttributes("user")
@Controller
public class SessionController {
@ModelAttribute("user")
public User getUser() {
User user = new User();
return user;
}
/**
* http://localhost:8080/s/session?id=1
* 请求转发 forward: 不需要任何处理
*
* 请求重定向 redirect:
* 使用SessionAttribute方式 用于在重定向中传至 将值存储在session中 【用完记住清除】
* @param map
* @return
* @throws Exception
*/
@RequestMapping(value = "/session", method = RequestMethod.GET)
public String case1(@ModelAttribute("user") User user) throws Exception {
return "redirect:/session2";
}
@RequestMapping(value = "/session2", method = RequestMethod.GET)
public String case2(Map map, HttpServletResponse res,SessionStatus sessionStatus) throws Exception {
User user = (User) map.get("user");
res.getWriter().println(user.getId());
sessionStatus.setComplete();
return null;
}
}