Controller中值返回頁面有四種方法。分別是使用ModelAndView,Map,Model和HTTPServletRequest。分爲兩大類。
第一大類是使用ModelAndView。因爲他的返回值是ModelAndView。方法:
@RequestMapping("/userinfo/userInfoView.do")
public ModelAndView userInfoView() {
ModelAndView mv = new ModelAndView("userinfo/userInfoView");
UserInfo info = new UserInfo();
info.setName("laxi");
info.setMobile(110);
mv.addObject("ui", info);
return mv;
}
第二大類是使用Map,Model和HTTPServletRequest,他們的返回值爲String視圖名。方法:
@RequestMapping("/userinfo/userInfoView1.do")
public String userInfoView1(Map<String,Object> map) {
UserInfo info = new UserInfo();
info.setName("jim");
info.setMobile(110);
map.put("ui", info);
return "userinfo/userInfoView";
}
@RequestMapping("/userinfo/userInfoView2.do")
public String userInfoView2(Model model) {
UserInfo info = new UserInfo();
info.setName("jim");
info.setMobile(110);
model.addAttribute("ui", info);
return "userinfo/userInfoView";
}
@RequestMapping("/userinfo/userInfoView3.do")
public String userInfoView3(HttpServletRequest request) {
UserInfo info = new UserInfo();
info.setName("jim");
info.setMobile(110);
request.setAttribute("ui", info);
return "userinfo/userInfoView";
}