Announcement Announcement Module
Collapse
No announcement yet.
@SessionAttribute problems Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @SessionAttribute problems

    I'm trying to store a variable in the session using the @SessionAttribute variable but I think I'm doing something wrong.

    The first class is the controller that is executed when you get all children for a given parent object and stores the selectedParent in the session to go back to later on.

    Code:
    @Controller
    @SessionAttributes(value={"selectedParent"})
    public class ParentController {
           // omitted members and autowired constructor
    	@RequestMapping("/admin/manageParents.htm")
    	public String manage(@RequestParam("id") Long parentId, ModelMap model) {
    		Parent parent = parentService.getParent(parentId);
    	
    		List<Child> children = Service.getChildren(parentId);
    		model.addAttribute("children", children);
    		model.addAttribute("selectedParent", selectedParent); // set this in the session
    		return "manageChildren";
    	}
    }
    and in the second controller, which is triggered when the user wants to delete one of the children that were previously loaded, the user should be returned to the list of children, which is reloaded with the id of the selectedParent, but there this sessionattribute seems to be null.

    Code:
    @Controller
    @RequestMapping("/admin/deleteChimd.htm")
    @SessionAttributes(value={"selectedParent"})
    public class DeleteChildController {
    	// omitted members and autowired constructor
    	@RequestMapping(method=RequestMethod.POST)
    	public String delete(@RequestParam("childId") Long childId, ModelMap model) {
    	        childService.deleteChild(childId);
    		Parent parent = (Parent) model.get("selectedParent"); // this appears to be NULL
    		return "redirect:manageChildren.htm?id="+parent.getId(); // NPE
    	}
    }
    so I wonder what the proper usage is of the sessionAttribute annotation
    Last edited by incubator; Feb 2nd, 2008, 07:35 AM.

  • #2
    It would be really great if somebody explained how @SessionAttributes worked because there are a lot of confused people people who are having trouble with it.

    Comment


    • #3
      have you tried:

      Code:
      @RequestMapping(method=RequestMethod.POST)
      	public String delete(@RequestParam("childId") Long childId, @ModelAttribute("parent") Parent parent, ModelMap model) {
      	        childService.deleteChild(childId);
      	        return "redirect:manageChildren.htm?id="+parent.getId(); // NPE (hopefully not anymore)
      	}

      Comment


      • #4
        The @SessionAttributes works in the same way as the sessionForm of the SimpleFormController. It puts the command (or for the @SessionAttributes any object) in the session for the duration between the first and the last request (most of the time the initial GET and the final POST). After that the stuff is removed.

        Each Controller has it's own ModelMap so something put as a @SessionAttributes in controller1 isn't available in controller2 and vice versa. For that to work you will have to put stuff on the session manually yourself.

        You might want to consider to put your stuff in one (MultiAction) Controller, that way you share all the Model stuff.

        Comment


        • #5
          Fantastic - this is what I needed.

          Somebody should just copy & paste that into the documentation.

          The nomenclature is a bit confusing - you see @SessionAttributes and you think HttpSession.getAttribute() and HttpSession.setAttribute().

          Comment


          • #6
            Originally posted by mdeinum View Post
            The @SessionAttributes works in the same way as the sessionForm of the SimpleFormController. It puts the command (or for the @SessionAttributes any object) in the session for the duration between the first and the last request (most of the time the initial GET and the final POST). After that the stuff is removed.

            Each Controller has it's own ModelMap so something put as a @SessionAttributes in controller1 isn't available in controller2 and vice versa. For that to work you will have to put stuff on the session manually yourself.

            You might want to consider to put your stuff in one (MultiAction) Controller, that way you share all the Model stuff.
            I'd love to put it in a multiaction controller but I dont know how to mix requestmappings with a form with mappings without.

            If I knew then I could easily make a multiaction controller for all CRUD operations

            Comment


            • #7
              Unless I'm misunderstanding your question:

              Code:
              @RequestMapping("/withForm.do")
              public String requestMappingWithForm(@ModelAttribute("someForm") SomeForm someForm, BindingResult result, SessionStatus status) {
               ...
              }
              
              @RequestMapping("/withoutForm.do")
              public String requestMappingWithoutForm(ModelMap model) {
               ...
              }

              Comment


              • #8
                thats all?
                hmmm, if that is combinable with the RequestMethod.GET & POST this sounds interesting.
                Thanks

                Comment


                • #9
                  how to put stuff in the session manually yourself.

                  Hello,

                  I am running into the same problem, and I would like to put the stuff in the session manually myself, but I don't know how to reference the session object, sense there is no reference to the httprequest object using anymore when using annotated spring mvc.

                  Comment


                  • #10
                    How to manually access the session while using annotated spring mvc

                    Hi,

                    I am guessing others are will find this useful, it turns out that you can just ask for the request of session objects in your annotated controllers... For example if you wanted the session or request object in the method below you could change this:

                    Code:
                    @RequestMapping(method = RequestMethod.GET)
                    public String handleRequest(String pageId, ModelMap model) {
                    to this:

                    Code:
                    @RequestMapping(method = RequestMethod.GET)
                    public String handleRequest(String pageId, HttpSession session, HttpServletRequest request,  ModelMap model) {
                    You don't need to ask for both the request and session (you can pick one or the other), and it will work the old school way we are all more used to!

                    Ping this thread if you found this helpful!

                    Comment


                    • #11
                      Originally posted by Marten Deinum View Post
                      The @SessionAttributes works in the same way as the sessionForm of the SimpleFormController. It puts the command (or for the @SessionAttributes any object) in the session for the duration between the first and the last request (most of the time the initial GET and the final POST). After that the stuff is removed.

                      Each Controller has it's own ModelMap so something put as a @SessionAttributes in controller1 isn't available in controller2 and vice versa. For that to work you will have to put stuff on the session manually yourself.

                      You might want to consider to put your stuff in one (MultiAction) Controller, that way you share all the Model stuff.
                      It would be nice if this explanation would be written in the doc.

                      Comment


                      • #12
                        Originally posted by Clay View Post
                        Fantastic - this is what I needed.

                        Somebody should just copy & paste that into the documentation.

                        The nomenclature is a bit confusing - you see @SessionAttributes and you think HttpSession.getAttribute() and HttpSession.setAttribute().
                        I agree with you... it's confusing!
                        The name could be something like '@SessionFormAttributes' to remember 'sessionForm' (AbstractFormController and subclasses), that is, the attributes of the session of some specific form controller.

                        Comment

                        Working...
                        X