2013年11月19日星期二

Propagation of the persistence of the HttpSession object data a little problem !

General site after landing in the HttpSession will save the user 's name or ID --- This is the background :
main line and today tried to pass using HttpSession object is the object database persistence , the code is as follows:
@RequestMapping(value = "/azzurro", method = RequestMethod.POST)
public String login(@RequestParam("account") String account,
@RequestParam("password") String password,
HttpServletRequest request, ModelMap modelMap) throws Exception {

String loginFlag = null;
User user = userService.getByAccount(account);

HttpSession session = request.getSession();



if (user == null) {

loginFlag = "INVALID";

} else {

LoginInfo loginInfo = user.getLoginInfo();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String lastLoginDate = sdf.format(loginInfo.getLastLoginDate()); //获取用户上次登陆日期(测试用)
session.setAttribute("loginInfo", loginInfo);

if(!user.isActive()) {

modelMap.put("loginFlag", "LOCKED");
return "login";
} else if ("choiland".equals(password)) {

userService.login(user, request.getSession());
} else if (!userService.login(account, password, request)) {

loginFlag = "INVALID";
}
}

if (loginFlag == null) {

return "redirect:/authorized/index";
} else {

modelMap.put("loginFlag", loginFlag);
return "login";
}
}

Then I go to another Controller get this loginInfo, the code is as follows:
@RequestMapping(value = "/welcome")
public String welcome(HttpSession session, ModelMap modelMap) {

User user = userService.getCurrent(session);

modelMap.put("account", user.getAccount());
LoginInfo loginInfo = (LoginInfo) session.getAttribute("loginInfo");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String lastLoginDate = sdf.format(loginInfo.getLastLoginDate()); //这里取出来的日期却是当前的时间
modelMap.put("loginInfo", loginInfo);

return "/authorized/index/welcome";
}

Question: What is the HttpSession save persistent object , the saved data is referenced or copied directly ? Why am I an HttpSession object automatically sync ?
------ Solution ---------------------------------------- ----
Java and the assignment operator in C + + is not the same . In C + + , this statement : bc2 = bc1; would an object called bc1 all data copied to an object called bc2 . That this statement is executed, the program has two objects contain the same data . However, in Java, this same assignment statement is only copied to the bc1 bc2 memory address pointed to , now bc1 and bc2 actually refers to the same object they are all references to the object . This greatly improves the memory usage efficiency, but also easy for some deep understanding of the memory bring some friends with the error . For example bc1.add (25); buc2.add (20); executed bc1 increase of 45 . The author may be intended just let bc1 increased 25, bc2 increase of 20 only. This explains , in Java , bc2 = bc1; does not truly replicate . So how in Java object copy it ? Please refer to:
on Java object replication
----- - For reference only ---------------------------------------
session bottom is map to achieve.
So you understand the key and value into the map 's very clear .
------ For reference only -------------------------------------- -

Bamboo help me greatly !
before me is to do in C + + , on this point has been a little fuzzy , now understand !
listen to Jun 's words, better than reading ten books !

没有评论:

发表评论