Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using Spring 3.2.0 MVC. In that i have to store one object to session. Currently i am using HttpSession set and get attribute to store and retive the value. It returns only the String not Object. I want to use @SessionAttribute when i tried it sets the object in session but i could not retrieve the session object

 @RequestMapping(value = "/sample-login", method = RequestMethod.POST)
    public String getLoginClient(HttpServletRequest request,ModelMap modelMap) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        User user = sample.createClient(userName, password);
        modelMap.addAttribute("userObject", user);
        return "user";
    }


     @RequestMapping(value = "/user-byName", method = RequestMethod.GET)
    public
    @ResponseBody
    String getUserByName(HttpServletRequest request,@ModelAttribute User user) {

        String fas= user.toString();
        return fas;
    }

both methods are in same controller. How would i use this to retrieve the object

share|improve this question
 
You worded the question as if you have code attempting to use @SessionAttribute, but your code snippet doesn't contain it. Therefore, how did you use it? –  sjngm Jul 18 '13 at 11:53
 
I added @SessionAttributes("userObject") i used this in my code –  jackyesind Jul 18 '13 at 11:56
add comment

1 Answer

up vote 3 down vote accepted

@SessionAttributes annotation are used on the class level to :

  1. Mark a model attribute should be persisted to HttpSession after handler methods are executed
  2. Populate your model with previously saved object from HttpSession before handler methods are executed -- if one do exists

So you can use it alongside your @ModelAttribute annotation like in this example:

@Controller
@RequestMapping("/counter")
@SessionAttributes("mycounter")
public class CounterController {

  // Checks if there's a model attribute 'mycounter', if not create a new one.
  // Since 'mycounter' is labelled as session attribute it will be persisted to
  // HttpSession
  @RequestMapping(method = GET)
  public String get(Model model) {
    if(!model.containsAttribute("mycounter")) {
      model.addAttribute("mycounter", new MyCounter(0));
    }
    return "counter";
  }

  // Obtain 'mycounter' object for this user's session and increment it
  @RequestMapping(method = POST)
  public String post(@ModelAttribute("mycounter") MyCounter myCounter) {
    myCounter.increment();
    return "redirect:/counter";
  }
}

Also don't forget common noobie pitfall: make sure you make your session objects Serializable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.