访问ADF页面时,如果超过在web.xml中的设置session-timeout的时间,没有任何动作的话,会弹出如下窗口:
图1
点击后,会重新刷新当前页面。
如果,在web.xml中增加oracle.adf.view.rich.sessionHandling.WARNING_BEFORE_TIMEOUT设置如下:
<context-param>
<param-name>oracle.adf.view.rich.sessionHandling.WARNING_BEFORE_TIMEOUT</param-name>
<param-value>120</param-value>
</context-param>
会在session过期之前的2分钟(120秒)时自动弹出如下窗口:
图2
点击确定,会重新刷新当前页面,并且session不会过期。如果没有理会该警告,那么在session过期之后,该窗口会自动关闭,然后弹出图1的窗口。
以上两个提示窗口是ADF默认提供的功能,一般来说,用户尚可接受。
如果把WARNING_BEFORE_TIMEOUT设置为0,那么session过期后,不会弹出任何窗口。
当用户在页面上进行任何操作时,会弹出如下窗口:
图3
点击确定后,页面显示如下:
图4
这当然不是我们所希望的。在实际需求中,用户可能不希望session过期时弹出任何窗口,而是重新刷新当前页面,那该怎么做呢?
我尝试了很多种办法,比如使用SessionListener,PhaseListener,Filter,ExceptionHandler。
最后发现,使用Filter可以解决用户的这个需求。
1. SessionTimeOutFilter.java
package view;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SessionTimeOutFilter implements Filter {
public SessionTimeOutFilter() {
super();
}
private FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void destroy() {
filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%% doFilter");
String requestedSession = ((HttpServletRequest)request).getRequestedSessionId();
String currentWebSession = ((HttpServletRequest)request).getSession().getId();
boolean sessionOk = currentWebSession.equalsIgnoreCase(requestedSession);
// if the requested session is null then this is the first application
// request and "false" is acceptable
if (!sessionOk && requestedSession != null) {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%% session has expired or renewed. Redirect request.");
// the session has expired or renewed. Redirect request
((HttpServletResponse)response).sendRedirect(filterConfig.getInitParameter("SessionTimeoutRedirect"));
} else {
chain.doFilter(request, response);
}
}
}
2. 在web.xml中增加如下配置
<context-param>
<param-name>oracle.adf.view.rich.sessionHandling.WARNING_BEFORE_TIMEOUT</param-name>
<param-value>0</param-value>
</context-param>
<filter>
<filter-name>SessionTimeOutFilter</filter-name>
<filter-class>view.SessionTimeOutFilter</filter-class>
<init-param>
<param-name>SessionTimeoutRedirect</param-name>
<param-value>index.jsf</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionTimeOutFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
3. 运行效果
当session过期后,不会有任何窗口弹出,用户在页面上进行任何操作时,会重新刷新当前页面。
我实验的结果还发现,后台会报出一个错误:
<RichExceptionHandler> <_logUnhandledException> ADF_FACES-60098:Faces 生命周期在阶段RESTORE_VIEW 1中接收到未处理的异常错误
java.lang.IllegalStateException: null windowId
查了一下,没找出原因,页面上没有任何影响,以后有时间再研究一下。
Project 下载:ADF_SessionTimeout.7z
1. http://www.baigzeeshan.com/2010/12/how-to-set-session-time-out-in-adf.html
2. http://www.baigzeeshan.com/2011/05/how-to-run-java-code-on-every-page-load.html
3. http://maverickshyam88.wordpress.com/2013/01/30/handling-session-time-out-in-adf/
4. http://www.baigzeeshan.com/2011/07/how-to-automatically-redirect-to.html
5. https://forums.oracle.com/thread/549724
6. https://cwiki.apache.org/confluence/display/MYFACES/Access+FacesContext+From+Servlet
7. http://www.coderanch.com/t/467358/JSF/java/access-faces-context-backing-beans
8. http://blog.olrichs.nl/2013/05/support-for-multiple-session-timeouts.html
9. http://adfdevelopers.blogspot.ch/2009/06/detecting-and-handling-user-session.html
10. http://prsync.com/oracle/tips-on-dealing-with-session-time-out-popup-522231/
11. https://forums.oracle.com/message/4239840
12. https://forums.oracle.com/thread/2437276
13. http://ramannanda.blogspot.jp/2013/04/adf-exception-handling-scenario.html
14. https://forums.oracle.com/thread/2358909
15. http://stackoverflow.com/questions/2543094/how-to-redirect-to-index-page-if-session-time-out-happend-in-jsf-application
没有评论:
发表评论