Reputation: 1035
Doing a web site using spring mvc. I added a SignupController to handle all the sign up related requests. Once user signup I am adding that to a session using @Scope("session"). Below is the SignupController code,
@Controller
@Scope("session")
public class SignupController {
@Autowired
SignupServiceInter signUpService;
private static final Logger logger = Logger.getLogger(SignupController.class);
private String sessionUser;
@RequestMapping("/SignupService")
public ModelAndView signUp(@RequestParam("userid") String userId,
@RequestParam("password") String password,@RequestParam("mailid") String emailId){
logger.debug(" userId:"+userId+"::Password::"+password+"::");
String signupResult;
try {
signUpService.registerUser(userId, password,emailId);
sessionUser = userId; //adding the sign up user to the session
return new ModelAndView("userHomePage","loginResult","Success");
//Navigate to user Home page if everything goes right
} catch (UserExistsException e) {
signupResult = e.toString();
return new ModelAndView("signUp","loginResult", signupResult);
//Navigate to signUp page back if user does not exist
}
}
}
I am using "sessionUser" variable to store the signed up User Id. My understanding is that when I use @Scope("session") for the controller all the instance variables will added to HttpSession. So by that understanding I tried to access this "SessionUser" in userHomePage.jsp as,
Welcome to <%=session.getAttribute("sessionUser")%>
But it throws null.
So my question is how to use the values from session variables in jsp pages that got saved using @Scope("session") in the mvc controllers.
Note: My work around is that pass that signed User Id to jsp page through ModelAndView, but it seems passing the value like these among the pages takes me back to managing state among pages using QueryStrings days.
Upvotes: 2
Views: 24214
Reputation: 49935
When you specify @Scope("session") for the controller, Spring will now ensure that there is a unique instance of the controller for every session, and so you will be able to keep a session specific state like sessionUser as a instance variable.
However to expose a variable to the UI, that variable has to be a part of the model/session attributes, you can do that either with @ModelAttribute("sessionUser") or @SessionAttribute("sessionUser")
@Controller
@SessionAttribute("sessionUser")
public class SignupController {
@RequestMapping("/SignupService")
public ModelAndView signUp(@RequestParam("userid") String userId,
@RequestParam("password") String password,@RequestParam("mailid") String emailId, Model model){
...
try {
signUpService.registerUser(userId, password,emailId);
sessionUser = userId; //adding the sign up user to the session
model.addAttribute("sessionUser", sessionUser);
return new ModelAndView("userHomePage","loginResult","Success");
//Navigate to user Home page if everything goes right
....
}
}
Upvotes: 2
Reputation: 12189
I've never tried spring integration with jsp, as I've been always using jsf, but I would give this a try:
First: add getter and setter for the property you're interested in (to the controller).
@Controller
@Scope("session")
public class SignupController {
...
private String sessionUser;
public void setSessionUser(String sessionUser) {
this.sessionUser = sessionUser;
}
public String getSessionUser() {
return this.sessionUser;
}
...
then: call it in jsp like this:
Welcome to ${session.sessionUser}
does that work for you? :) If not, I'd try with:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
Welcome to <c:out value="${session.sessionUser}" />
Upvotes: 0