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>