在IndexController里面,从数据库中取出用户信息到UserVo中,然后把它放到model和session中
@Controller
public class IndexController {protected Logger LOGGER = Logger.getLogger(getClass());
@Resource(name = "com.niukou.db.daoimpl.UserDao")
private IUserDao userDao;
@RequestMapping(value = "index.html")
public ModelAndView index(HttpSession session){
UserVo userVo = userDao.getUserById(1);
System.out.println(userVo.getUserName());
Map model = new HashMap<String, Object>();
session.putValue("userVo", userVo);
model.put("userVo",userVo);
return new ModelAndView("index", model);
}
}
<form action="login.html" method="post"> <input type="submit" value="submit"> </form>
在TestContoller中加上@SessionAttributes
在方法体中加入@ModelAttribute用于获取从IndexController传过来的UserVo
@Controller @SessionAttributes({"userVo"}) public class TestController { @RequestMapping(value="login.html") public ModelAndView login(HttpServletRequest request, @ModelAttribute("userVo")UserVo userVo){ System.out.println(userVo.getUserName()); System.out.println(userVo.getUserId()); Map model = new HashMap<String, Object>(); userVo.setUserName("brady"); model.put("userVo", userVo); return new ModelAndView("login", model); }