How to use the values from session variables in jsp pages that got saved using @Scope("session") in the mvc controllers

Posted by droidsites on Stack Overflow See other posts from Stack Overflow or by droidsites
Published on 2012-08-27T21:24:12Z Indexed on 2012/08/27 21:38 UTC
Read the original article Hit count: 132

Filed under:
|

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,

SignupController.java

@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,

userHomepage.jsp

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.

© Stack Overflow or respective owner

Related posts about spring

Related posts about spring-mvc