JSF-javax.faces.application.ViewExpiredException:

本文介绍了解决JSF应用中视图过期异常的方法。针对不同Web容器如Sun App Server和Tomcat,提供了具体的配置代码示例。通过在web.xml中设置错误页或者自定义生命周期实现,可以优雅地处理session超时导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

javax.servlet.ServletException: viewId:/login.jsp - View /login.jsp could not be restored. javax.faces.webapp.FacesServlet.service(FacesServlet.java:249) root cause javax.faces.application.ViewExpiredException: viewId:/login.jsp - View /login.jsp could not be restored. com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:185) com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117) javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)

出现这个错误是因为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>

02-Apr-2025 17:22:57.571 信息 [RMI TCP Connection(2)-127.0.0.1] org.primefaces.webapp.PostConstructApplicationEventListener.processEvent Running on PrimeFaces 6.1 02-Apr-2025 17:22:57.625 信息 [RMI TCP Connection(2)-127.0.0.1] com.sun.faces.config.ConfigureListener.contextInitialized 初始化上下文 '' 的 Mojarra 2.1.7 (SNAPSHOT 20120206) 02-Apr-2025 17:22:57.873 信息 [RMI TCP Connection(2)-127.0.0.1] com.sun.faces.spi.InjectionProviderFactory.createInstance JSF1048:有 PostConstruct/PreDestroy 注释。标有这些注释的 ManagedBeans 方法将表示注释已处理。 02-Apr-2025 17:23:00.343 警告 [RMI TCP Connection(2)-127.0.0.1] com.sun.faces.mgbean.BeanManager.addBean JSF1074:名为 'customizedDocumentsView' 的受管 bean 已注册。将现有受管 bean 类类型 com.spy.framework.utils.CustomizedDocumentsView 替换为 com.spy.framework.utils.CustomizedDocumentsView。 02-Apr-2025 17:23:00.602 信息 [RMI TCP Connection(2)-127.0.0.1] org.primefaces.webapp.PostConstructApplicationEventListener.processEvent Running on PrimeFaces 6.1 02-Apr-2025 17:23:00.602 信息 [RMI TCP Connection(2)-127.0.0.1] org.primefaces.webapp.PostConstructApplicationEventListener.processEvent Running on PrimeFaces 6.1 02-Apr-2025 17:23:00.602 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal 一个或多个listeners启动失败,更多详细信息查看对应的容器日志文件 02-Apr-2025 17:23:00.606 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal 由于之前的错误,Context[]启动失败 02-Apr-2025 17:23:00.687 严重 [RMI TCP Connection(2)-127.0.0.1] javax.faces.FactoryFinder$FactoryManager.getFactory 启动时没有正确初始化应用程序,无法找到工厂:javax.faces.application.ApplicationFactory. Attempting to find backup. 02-Apr-2025 17:23:00.693 严重 [RMI TCP Connection(2)-127.0.0.1] com.sun.faces.config.ConfigureListener.contextDestroyed Unexpected exception when attempting to tear down the Mojarra runtime java.lang.IllegalStateException: Could not find backup for factory javax.faces.application.ApplicationFactory. at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1008) at jav
最新发布
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值