我正在使用一个扩展org.apache.wicket.protocol.http.WebSession的会话类; 由于注销或超时,我需要一个在会话到期时调用的方法.但我一无所获.我该怎么做?
您可以在Wicket级别上执行此操作:
通过覆盖SessionStore实现 - 覆盖Application#newSessionStore()
@Override
protected ISessionStore newSessionStore() {
return new SecondLevelCacheSessionStore(this, new DiskPageStore()) {
@Override
protected void onUnbind(String sessionId) {
// this code is called when wicket call httpSession.invalidate()
}
};
}
Run Code Online (Sandbox Code Playgroud)
但这有一个缺点:当会话到期时(由servlet容器控制),将不会调用此代码.换句话说 - 你只能处理由wicket本身引起的会话破坏事件.
在全局级别,您可以使用Servlet API的HttpSessionListener - 您可以对会话销毁事件做出反应,无论它是由哪个触发的
HttpSesionListener#sessionDestroyed(HttpSessionEvent se)
Run Code Online (Sandbox Code Playgroud)
并将其写入您的WEB-INF/web.xml
<listener>
<listener-class>
your.listener.class.full.qualified.name
</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)