数据处理模型

来源:互联网 发布:有什么聊天软件 编辑:程序博客网 时间:2024/06/06 18:57
数据处理模型:http://www.mamicode.com/info-detail-1142041.html

 * springmvc中Model相关对象 是处理和数据相关的对象
 * @ModelAttribute 重命名 参数数据
 * Model(ModelMap|Map)传递数据到视图(相当于request.setAttribute)
 * ModelAndView 绑定数据到视图 (ModelMap用于传递数据 View对象用于跳转)


@SessionAttributes("user")
@Controller
public class SessionController {


@RequestMapping(value="/case",method=RequestMethod.GET)
public String case1(Map map) throws Exception{
map.put("sex", "girl");
return "/lesson03/res.jsp";
}


@RequestMapping(value="/case2",method=RequestMethod.GET)
public ModelAndView case2() throws Exception{
//ModelAndView mav=new ModelAndView("/lesson03/res.jsp");
ModelAndView mav=new ModelAndView();
mav.setViewName("/lesson03/res.jsp");
mav.addObject("sex", "boy");
return mav;
}




@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;
}


}
原创粉丝点击