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>


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 zhenggp
 */
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 zhenggp
 */
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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值