Roma Kap
Roma Kap

Reputation: 636

Spring MVC Session delete attribute over multiple requests

i saw here a lot of issues about deletion of session-attributes, which works fine, but it is not what i want. Maybe i misunderstand this.

Problem: I send two ajax-requests over one session. When i send first ajax-request "RequestA", my attribute "AtributeA" will be deleted. I see, that within hattpSession it is not listed anymore. When i send second ajax-request "RequestB" to some other handler, i still see my attribute "AtributeA". Even hash-value from this object is the same like in "RequestA". Maybe i make something wrong or it is normal behavior?

My Code by first ajax-request with Requestmapping: "routeA":

@Controller
@SessionAttributes({"ObjectAA"}){
       @RequestMapping("/routeA")
        public handlerAA(HttpSession session){
              session.removeAttribute("ObjektAA"); //Attribute will be deleted as expected
        }

        @RequestMapping("/routeB")
         public handlerBB(HttpSession session){
               session.getAttribute("ObjektAA");
        }
}

My Code by second ajax-request with Requestmapping: "routeB":

@Controller
@SessionAttributes({"ObjectAA"}){
       @RequestMapping("/routeA")
        public handlerAA(HttpSession session){
              session.removeAttribute("ObjectAA");
        }


        @RequestMapping("/routeB")
         public handlerBB(HttpSession session){
               session.getAttribute("ObjectAA");  //ObjectAA is still in the session? how could it be?
        }
}

Upvotes: 0

Views: 1437

Answers (1)

Vivek Nerle
Vivek Nerle

Reputation: 304

Check this spring document, it will clear your problem.

org.springframework.web.bind.annotation.SessionAttributes

Annotation that indicates the session attributes that a specific handler uses. This will typically list the names of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans. Declared at the type level, applying to the model attributes that the annotated handler class operates on. NOTE: Session attributes as indicated using this annotation correspond to a specific handler's model attributes, getting transparently stored in a conversational session. Those attributes will be removed once the handler indicates completion of its conversational session. Therefore, use this facility for such conversational attributes which are supposed to be stored in the session temporarily during the course of a specific handler's conversation. For permanent session attributes, e.g. a user authentication object, use the traditional session.setAttribute method instead. Alternatively, consider using the attribute management capabilities of the generic org.springframework.web.context.request.WebRequest interface. NOTE: When using controller interfaces (e.g. for AOP proxying), make sure to consistently put all your mapping annotations — such as @RequestMapping and @SessionAttributes — on the controller interface rather than on the implementation class. Since: 2.5 Author: Juergen Hoeller Sam Brannen

If you found it helpful mark it as helpful. #happycoding

Upvotes: 1

Related Questions