PDA

View Full Version : Spring 2.0 scope="session"


titiwangsa
Jul 13th, 2007, 10:30 AM
hello to all
is there a way, that my bean
can get the current session id if i put the scope="request" or scope="session"?
i mean without access to the HttpSession or HttpServletRequest objects?

is there something similar to this


class MyBean implements HttpSessionAware
{
private String sessionId;
public void setHttpSession(HttpSession httpSession)
{
sessionId = httpSession.getSessionId();
}

}


and

<bean id="myBean" class="MyBean" request="session">
</bean>


so when the bean is created, i can get the session id?
goal: when the bean is created, i can have the session id.
any way i can accomplish this, or something similar?

titiwangsa
Jul 13th, 2007, 10:11 PM
i think i can get the current session from this

private RequestAttributes getRequestAttributes() {
return RequestContextHolder.currentRequestAttributes();
}

private String getSessionId() {
return getRequestAttributes().getSessionId();
}



as long as my object is in the same thread or a thread that is inherited from the DispatcherServlet, everything will work out ok.