Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @SessionAttribute how to retrieve the session object in same controller

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 retrieve 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?

like image 295
jackyesind Avatar asked Jul 18 '13 11:07

jackyesind


People also ask

How can get a session object in Spring?

Getting HttpSession Object in Spring Controller is very easy . Just Put it as a method parameter in controller method and Spring will automatically inject it . There is another approach where we create Session scoped Controller . This Controller get created for each session and controller object is stored in session.

Where are session attributes stored?

For completed session searches, session attributes are stored in the [CanisterSummary] section of the request, where they are automatically indexed.

How can I see session attributes?

Click the Application tab to open the Application panel. Expand the Session Storage menu. Click a domain to view its key-value pairs. Click a row of the table to view the value in the viewer below the table.


1 Answers

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

like image 54
gerrytan Avatar answered Sep 20 '22 04:09

gerrytan