日期:2014-05-16  浏览次数:20364 次

jsf 中 ViewExpiredException 的处理
在jsf 中,存在 session 的地方,一般会去使用session 去保存数据或者其他的事情,但是当session time out or forced to be invalidate  , 就会抛出这样的 ViewExpiredException 异常。

如果是 web server == sun app server
then solution is :
in web.xml :
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/sessionExpired.jsp</location>
</error-page>


in sessionExpired.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@include file="/WEB-INF/jspf/taglibs.jspf" %>

<c:redirect url="/login.jsf" />



在jsf 中,存在 session 的地方,一般会去使用session 去保存数据或者其他的事情,但是当session time out or forced to be invalidate  , 就会抛出这样的 ViewExpiredException 异常。

如果是 web server == sun app server

then solution is ::

in web.xml :
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/sessionExpired.jsp</location>
</error-page>



sessionExpired.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@include file="/WEB-INF/jspf/taglibs.jspf" %>

<c:redirect url="/login.jsf" />


but if in tomcat container ,the solution is some complicated :
package trackingmap;

import com.sun.faces.lifecycle.LifecycleImpl;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author chenwq
 */
public class TeleLifecycleImpl extends LifecycleImpl{
    
    /** Creates a new instance of TeleLifecycleImpl */
    public TeleLifecycleImpl() {
        super();
    }
     
    public void execute(FacesContext context) throws FacesException{
        try{
            super.execute(context);
        }catch(javax.faces.application.ViewExpiredException e){
            System.out.println("catch ViewExpiredException here");
            try{
                context.responseComplete();
                context.renderResponse();
                HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
                String url = context.getExternalContext().getRequestContextPath() + "/faces/Login.jsp";///sessionExpired.jsp";
                response.sendRedirect(url);
            }catch(Exception e1){
                System.out.println("url redirect wrong ");
            }
            //throw e;
        }catch(FacesException ex){
            throw ex;
        }
        
    } 
    
}


package trackingmap;
import com.sun.faces.lifecycle.LifecycleFactoryImpl;
import javax.faces.lifecycle.Lifecycle;
/**
 *
 * @author chenwq
 */
public class TeleLifecycleFactoryImpl extends LifecycleFactoryImpl {
    public final static String CUSTOM_LIFECYCLE = "TELEEPOCH";    
    /** Creates a new instance of TeleLifecycleFactoryImpl */
    public TeleLifecycleFactoryImpl() {
        addLifecycle(CUSTOM_LIFECYCLE,new TeleLifecycleImpl());
    }
  
    
}


add into faces-config.xml :
<factory>
 <lifecycle-factory>trackingmap.TeleLifecycleFactoryImpl</lifecycle-factory>
</factory>


also add into web.xml
<context-param>
      <param-name>javax.faces.LIFECYCLE_ID</param-name>
     <param-value>TELEEPOCH</param-value>
</context-param>