`
yuzhi2217
  • 浏览: 34945 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JSF的ViewExpiredException异常处理 转

    博客分类:
  • JSF2
阅读更多

视图没有存储的异常

javax.faces.application.ViewExpiredException: viewId:/pages/gardeninfo/envRiskEditForm.faces - View /pages/gardeninfo/envRiskEditForm.faces could not be restored.

对于用户来说是不懂的,那只要在重写生命周期便可处理

第一种:

 1. 重写一个类,继承Lifecycle类,重写父类的execute方法,捕捉该异常,然后进行处理(如设置跳转到登录页面)。
2. 重写一个类,继承LifecycleFactoryImpl类,添加SessionLifecycleImpl的实例到工厂中,假设重写的子类为project.jsf.test.SessionLifecycleFactoryImpl。
3.在faces-config.xml中注册JSFLifecycleFactory类。
4.在web.xml中指定JSFLifecycleFactory工厂中创建的JSFLifecycle实例的键(key)。

public class JSFLifecycleFactory extends LifecycleFactoryImpl {
 private static Logger LOGGER = FacesLogger.LIFECYCLE.getLogger();

 public static final String JSF_LIFECYCLE = "JSF";

 public JSFLifecycleFactory() {
  super();
 
  lifecycleMap.put(JSF_LIFECYCLE, new JSFLifecycle());
  if (LOGGER.isLoggable(Level.FINE)) {
   LOGGER.fine("Created EJS JSF Lifecycle");
  }
 }
}

public class JSFLifecycle extends Lifecycle {

 private static Logger LOGGER = FacesLogger.LIFECYCLE.getLogger();

 private Phase response = new RenderResponsePhase();

 private Phase[] phases = {
   null, // ANY_PHASE placeholder, not a real Phase
   new RestoreViewPhase(), new ApplyRequestValuesPhase(),
   new ProcessValidationsPhase(), new UpdateModelValuesPhase(),
   new InvokeApplicationPhase(), response };

 private List<PhaseListener> listeners = new CopyOnWriteArrayList<PhaseListener>();

 public void execute(FacesContext context) throws FacesException {
  try {
   if (context == null) {
    throw new NullPointerException(MessageUtils
      .getExceptionMessageString(
        MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
        "context"));
   }

   if (LOGGER.isLoggable(Level.FINE)) {
    LOGGER.fine("execute(" + context + ")");
   }

   for (int i = 1, len = phases.length - 1; i < len; i++) {
    if (context.getRenderResponse()
      || context.getResponseComplete()) {
     break;
    }
    phases[i].doPhase(context, this, listeners.listIterator());
   }
  }
  catch (ViewExpiredException e) {
   JSFMessageUtils.removeMessage();
   JSFMessageUtils.showAndLogException("信息","操作错误", e
       .getMessage(), null, JSFMessageUtils.WARN,
     JSFMessageUtils.OK);
  }
  catch (Exception e) {
   JSFMessageUtils.removeMessage();
   JSFMessageUtils.showAndLogException("信息","操作错误", e
     .getMessage(), null, JSFMessageUtils.WARN,
   JSFMessageUtils.OK);
  }
 }

 public void render(FacesContext context) throws FacesException {
  try {
   if (context == null) {
    throw new NullPointerException(MessageUtils
      .getExceptionMessageString(
        MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
        "context"));
   }

   if (LOGGER.isLoggable(Level.FINE)) {
    LOGGER.fine("render(" + context + ")");
   }
   if (!context.getResponseComplete()) {
    response.doPhase(context, this, listeners.listIterator());
   }
  }
  catch (FacesException e) {
   JSFMessageUtils.removeMessage();
   JSFMessageUtils.showAndLogException("信息","操作错误", e
     .getMessage(), null, JSFMessageUtils.WARN,
   JSFMessageUtils.OK);
  }
  catch (Exception e) {
   JSFMessageUtils.showAndLogException("信息","操作错误", e
     .getMessage(), null, JSFMessageUtils.WARN,
   JSFMessageUtils.OK);
  }
 }

 public void addPhaseListener(PhaseListener listener) {
  if (listener == null) {
   throw new NullPointerException(MessageUtils
     .getExceptionMessageString(
       MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
       "listener"));
  }

  if (listeners == null) {
   listeners = new CopyOnWriteArrayList<PhaseListener>();
  }

  if (listeners.contains(listener)) {
   if (LOGGER.isLoggable(Level.FINE)) {
    LOGGER.log(Level.FINE,
      "jsf.lifecycle.duplicate_phase_listener_detected",
      listener.getClass().getName());
   }
  }
  else {
   if (LOGGER.isLoggable(Level.FINE)) {
    LOGGER.log(Level.FINE, "addPhaseListener({0},{1})",
      new Object[] { listener.getPhaseId().toString(),
        listener.getClass().getName() });
   }
   listeners.add(listener);
  }
 }

 public PhaseListener[] getPhaseListeners() {
  return listeners.toArray(new PhaseListener[listeners.size()]);

 }

 public void removePhaseListener(PhaseListener listener) {
  if (listener == null) {
   throw new NullPointerException(MessageUtils
     .getExceptionMessageString(
       MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID,
       "listener"));
  }

  if (listeners.remove(listener) && LOGGER.isLoggable(Level.FINE)) {
   LOGGER.log(Level.FINE, "removePhaseListener({0})",
     new Object[] { listener.getClass().getName() });
  }
 }
}

 

faces-config.xml:

 

<factory>
  <lifecycle-factory>org.ejs.jsf.JSFLifecycleFactory</lifecycle-factory>
 </factory>

  web.xml

 <context-param>
  <param-name>javax.faces.LIFECYCLE_ID</param-name>
  <param-value>JSF</param-value>
 </context-param>s

 

第二种网上搜的:

 1. 重写一个类,继承LifecycleImpl类,重写父类的execute方法,捕捉该异常,然后进行处理(如设置跳转到登录页面),假设重写的子类名为project.jsf.test.SessionLifecycleImpl。
2. 重写一个类,继承LifecycleFactoryImpl类,添加SessionLifecycleImpl的实例到工厂中,假设重写的子类为project.jsf.test.SessionLifecycleFactoryImpl。
3.在faces-config.xml中注册SessionLifecycleFactoryImpl类。
4.在web.xml中指定SessionLifecycleFactoryImpl工厂中创建的SessionLifecycleImpl实例的键(key)。

具体如下:
1.project.jsf.test.SessionLifecycleImpl类:

import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import com.sun.faces.lifecycle.LifecycleImpl;

public class SessionLifecycleImpl extends LifecycleImpl {
public final static String SESSION_TIMEOUT_PAGES = "/pages/sessionTimeOut.faces";
public SessionLifecycleImpl() {
super();
}

public void execute(FacesContext context) {
try {
super.execute(context);
}catch(ViewExpiredException vee) {
redirect(context);
}catch(FacesException fe) {
throw fe;
}
}

private void redirect(FacesContext context) {
try {
context.responseComplete();
context.renderResponse();
HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();
String url = context.getExternalContext().getRequestContextPath()+ SESSION_TIMEOUT_PAGES;
response.sendRedirect(url);
}catch(Exception e) {
System.out.println(" Error: session timeout url redirect ");
}
}
}

2. project.jsf.test.SessionLifecycleFactoryImpl类:

import com.sun.faces.lifecycle.LifecycleFactoryImpl;

public class SessionLifecycleFactoryImpl extends LifecycleFactoryImpl {
public static final String SESSION_LIFECYCLE = "SessionLifecycle";
public SessionLifecycleFactoryImpl(){
super();
addLifecycle(SESSION_LIFECYCLE, new SessionLifecycleImpl());
}
}

3. faces-config.xml中注册SessionLifecycleFactoryImpl类
<faces-config>
<factory>
<lifecycle-factory>project.jsf.test.SessionLifecycleFactoryImpl</lifecycle-factory>
</factory>
.........
</faces-config>

4. web.xml中指定SessionLifecycleImpl实例的键(key):
<context-param>
<param-name>javax.faces.LIFECYCLE_ID</param-name>
<param-value>SessionLifecycle</param-value>
</context-param>

 

第三种:网上搜的

现这个错误是因为session 超时。当然关掉页面重新打开就不会有这个问题,但是在可用性方面就很差。作为开发人员看见这个错误会知道为什么,普通浏览者肯定会觉得出了什么问题。所以还是解决一下好。

如果是 sun appplication server

解决办法是在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">
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<c:redirect url="/login.jsf" />

如果web容器是tomcat,解决办法如下:

package com.jsf.util;

import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import com.sun.faces.lifecycle.LifecycleImpl;

public class TeleLifecycleImpl extends LifecycleImpl
{
 public TeleLifecycleImpl()
 {
  super();
 }
 
 public void execute(FacesContext context)
 {
  try
  {
   super.execute(context);
  }
  catch (ViewExpiredException  vee)
  {
   redirect(context);
  }
  catch (FacesException fe)
  {
   throw fe;
  }
 }
 
 private void redirect(FacesContext context)
 {
  try
  {
   context.responseComplete();
   context.renderResponse();
   HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
   String url = context.getExternalContext().getRequestContextPath() + "/faces/error.jsp";
   response.sendRedirect(url);
  }
  catch (Exception e)
  {
   System.out.println("url redirect wrong ");
  }

 }
}

 在jsf配置文件 faces-config.xml 中添加如下内容

<factory>
     <lifecycle-factory>trackingmap.TeleLifecycleFactoryImpl</lifecycle-factory>
</factory>

在web.xml 中添加如下内容

<context-param>
     <param-name>javax.faces.LIFECYCLE_ID</param-name>
     <param-value>TELEEPOCH</param-value>
</context-param>

分享到:
评论

相关推荐

    jsf2segy.zip

    浅剖数据jsf格式数据转换为segy数据,可以实现数据转换,以至于在其他软件上处理数据

    JSF如何监测和处理session超时

    自己在网上查询整理出来的,欢迎交流! 从 JSF1.2 规范说明中,我们可以发现在恢复视图的生命周期时会抛出ViewExpiredException 异常,因此我们可在恢复视图的生命周期前做一些处理。

    JSF Java Server Faces (JSF)框架

    JSF是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准)。JSF(Java Server Faces)技术为开发基于网络用户界面的Java开发者提供了标准的编程接口API以及标签库。就像Struts框架...

    jsf实例jsf实例 JSF学习 JSF jar包 JSF

    jsf实例 JSF学习 JSF jar包 JSF jsf实例 JSF学习 JSF jar包 JSFjsf实例 JSF学习 JSF jar包 JSF jsf实例 JSF学习 JSF jar包 JSF

    jsf分页 jsf分页 jsf分页

    jsf 分页 jsf 分页 jsf 分页 jsf 分页

    《JSF_实战》非常好的JSF学习书

    《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF学习书《JSF_实战》非常好的JSF...

    JSF基础教程 简体中文

    要开发 JSF 组件,您需要更深入了解 JSF 的一些处理细节,包括了 JSF 生命周期以及 JSF 框架。 o JSF 生命周期 o 概述自订组件 简单实例 在不考虑组件有子组件的情况下,这边以实际的一个例子来说明开发组件的过程...

    JSF中文教程jsf

    JSF中文教程JSF中文教程JSF中文教程JSF中文教程

    JSF的工作方式 JSF架构 JSF模型 JSF事件类型

    主要讲述JSF的工作方式 JSF的工作方式 JSF架构 JSF模型 JSF事件类型

    JSF开发介绍JSF开发介绍

    JSF开发介绍JSF开发介绍JSF开发介绍JSF开发介绍JSF开发介绍JSF开发介绍JSF开发介绍JSF开发介绍JSF开发介绍

    jsf第一个例子 jsf架包 jsf实例

    第一个JSF程序,初学JSF者必备,jsf架包 jsf实例

    core jsf 1.2 jsf 核心

    jsf1.2 core jsf jsf核心

    JSF开发必备JAR

    ================================= JSF开发必备JAR ================================= &lt;br&gt;《JSF入门简单中文版》开篇提到JSF开发需要的jar,' 但是按其中提供的方法: &lt;br&gt;jstl.jar 与 ...

    jsf视频jsf视频0

    jsf 视频 java faces jsf 视频 java faces jsf 视频 java faces

    JSF入门教程JSF入门教程

    JSF入门教程 JSF入门教程 JSF入门教程

    JSF请求处理生命周期图

    JSF请求处理生命周期图

    jsf 中文文档 jsf 中文文档

    jsf 中文文档jsf 中文文档jsf 中文文档jsf 中文文档

    jsf教程 JSF为JAVA的 Web应用用户界面

    JSF为JAVA的 Web应用用户界面的开发人员提供了标准的编程接口、丰富可扩展的UI组件库(一个核心的JSP标记库用来处理事件、执行验证以及其他非UI相关的操作和一个标准的HTML 标记库来表示 UI组件)、事件驱动模型等...

    JSF学习,JSF标签使用

    JSF的学习入门知识教程,里面有例子还有各个标签的使用及属性介绍

    jsf 文档 jsf

    jsf 文档 jsf 文档 jsf 文档 jsf 文档

Global site tag (gtag.js) - Google Analytics