Reputation: 109
I'm writing web application using SpringMVC. I would like to keep some data in session until user logout. Earlier I did it by @SessionAttributes("someData")
, but then I had to put @ModelAttribute("someData") someDataType someData
as argument for every request mapping method. So this is what I did :
My AccessData class :
@Component
@Scope(value = "session")
public class AccessData {
private long userID;
private String userKey;
public AccessData(long ID, String key) {
this.userID = ID;
this.userKey = key;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public String getUserKey() {
return userKey;
}
public void setUserKey(String userKey) {
this.userKey = userKey;
}
}
First controller(here I'm getting and validating user input from form) :
@Controller
@Scope(value = "session")
public class LoginController {
@ModelAttribute("accessData")
public AccessData getAccessData() {
return this.accessData;
}
private Utils utilsService;
private LoginService loginService;
@Autowired
private AccessData accessData;
@Autowired
public LoginController(LoginService loginService, Utils utils) {
this.loginService = loginService;
this.utilsService = utils;
}
@RequestMapping(value = ControllerPaths.LOGIN, method = RequestMethod.POST)
public ModelAndView showLoginStatus(
@ModelAttribute(LOGINDATA) LoginData loginData) {
try {
accessData = loginService.validateLoginData(loginData);
} catch (IncorrectLoginDataException e) {
logger.trace("Showing fail login screen...");
return utilsService.getShowView(ViewPaths.LOGIN_FAIL);
} catch (HTTPException e) {
return utilsService.getShowViewWithStringAttribute(
ViewPaths.INFO_VIEW, MESSAGE, CONNECTION_ERROR);
}
return utilsService.getShowView(ViewPaths.LOGIN_SUCCESS);
}
}
Second controller :
@Controller
@Scope(value = "session")
public class SecondController {
@ModelAttribute("accessData")
public AccessData getAccessData() {
return this.accessData;
}
private Utils utilsService;
private LoginService loginService;
@Autowired
private AccessData accessData;
@Autowired
public SecondController(LoginService loginService, Utils utils) {
this.loginService = loginService;
this.utilsService = utils;
}
@RequestMapping(value = ControllerPaths.SHOW_ACCESS_DATA, method = RequestMethod.GET)
public ModelAndView showAccessData(
System.out.println(accessData.getUserKey());
return utilsService.getShowView(ViewPaths.INDEX);
}
}
The problem is that when I'm printing userKey value in second controller, the value is null. I checked if I'm getting correct data from server in LoginController
and it's ok. So what am I doing wrong? Thanks in advance for help
Upvotes: 4
Views: 7183
Reputation: 21000
The problem is that you are assigning a new object to the accessData variable. Instead you should update the field of the object that it is already referring to.
accessData = loginService.validateLoginData(loginData);
// Replace above line with something like this. Implement the copyProperties
// method to copy the attributes you need
AccessData newAccessData = loginService.validateLoginData(loginData);
copyPropteries(accessData,newAccessData);
Also it is not required to make the Controllers session scoped, add proxyMode=TARGET_CLASS
to the scope annotation of the AccessData
class.
Upvotes: 2