Saeed
Saeed

Reputation: 616

how add attribute to session without having model object in spring mvc?

I have a class like:

@Controller(value = "services")
@RequestMapping("/")
@SessionAttributes({"SESSIONID"})
public class Services {

   @RequestMapping("/user/loginStatic")
   @ModelAttribute("SESSIONID")
   public LoginResponseBean loginStatic(String username){
       LoginResponseBean result = otherClass.login(username);
       retrun result;
   }
}

my problem: this code is cause to store "result" object in session but I want to store "result.getSessionId()" in session.

I can not add "Model model" to input argument of "loginStatic" method because it whould change the method signitaure and I cann't do it now. and also I can not get http session explicitely and set attribute in it (because of someother side effects). how could I do that? thanks..

Upvotes: 1

Views: 1981

Answers (2)

andyb
andyb

Reputation: 43823

You could use <mvc:interceptors/> to register a custom HandlerInterceptor which would apply to one, more or all controllers. For example, here is how to register an interceptor for all controllers:

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="a.b.c.MyHandlerInterceptorAdapter"/>
    </mvc:interceptor>
</mvc:interceptors>

Please also see mvc-config-interceptors and mvc-handlermapping-interceptor documentation for more details.

Note: Spring 3.2 documentation linked, so you may want to change the release number in the URL to match the version of Spring you are using.

Upvotes: 1

Vasyl Keretsman
Vasyl Keretsman

Reputation: 2748

Have a look at the accepted answer here: How do I get the session object in spring

You can get the session object as described there and add your attribute.

Upvotes: 0

Related Questions