首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewExpiredException错误页未显示

ViewExpiredException错误页未显示
EN

Stack Overflow用户
提问于 2014-02-15 16:15:24
回答 2查看 4.6K关注 0票数 5

我将一些选项卡与表单一起打开,当我按下命令按钮(会话结束后一段时间后),我会收到一个java脚本警告,它说:

serverError:类javax.faces.application.ViewExpiredException viewId:/javax.faces.application.ViewExpiredException. class视图/register.xhtml无法恢复。

火狐(本地玻璃鱼4)

我补充说:

代码语言:javascript
复制
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/index.xhtml?faces-redirect=true</location>
</error-page>

在我的web.xml中,但是我没有重定向到我的索引。那是为什么?

编辑:示例按钮

代码语言:javascript
复制
<h:commandButton value="Register" action="#{userController.register}">
    <f:ajax execute="@form" render="@form" />
</h:commandButton>

UserController是ViewScoped

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-18 10:37:17

如果你看看JSF2.2规范的13.3.6.3和13.3.6.4节,至少如果你有

代码语言:javascript
复制
<context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
</context-param>

在您的web.xml中(它无声地忽略了生产模式中的错误)。JSF客户端代码在单击该按钮并收到

代码语言:javascript
复制
<error>
    <error-name>...</error-name>
    <error-message>...</error-message>
</error>

您的ViewExpiredException在响应中。然后,客户端代码将对该错误信息作出反应。使用onerror (在f:ajax标记上)或addOnError (在jsf.ajax对象上)注册一个函数,以便自己处理错误。

由于很多人期望服务器在这样的问题上指导客户端,所以总括包括了FullAjaxExceptionHandler,它基本上重置了您的响应并发送您指定的错误页面。

还请参见:

JSF/PrimeFaces请求的会话超时和ViewExpiredException处理

票数 4
EN

Stack Overflow用户

发布于 2014-02-17 09:49:49

尝试此方法来处理视图过期:在项目中创建以下两个类

代码语言:javascript
复制
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;

public class ViewExpiredExceptionExceptionHandlerFactory extends
        ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

    public ViewExpiredExceptionExceptionHandlerFactory(
            ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new ViewExpiredExceptionExceptionHandler(
            parent.getExceptionHandler());
    }
}

代码语言:javascript
复制
public class ViewExpiredExceptionExceptionHandler extends
        ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public ViewExpiredExceptionExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getWrapped() {
        return this.wrapped;
    }

    @Override
    public void handle() throws FacesException {
        for (Iterator<ExceptionQueuedEvent> i = 
                getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
            ExceptionQueuedEvent event = i.next();
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)
                event.getSource();
            Throwable t = context.getException();
            if (t instanceof ViewExpiredException) {
                ViewExpiredException vee = (ViewExpiredException) t;
                FacesContext facesContext = FacesContext.getCurrentInstance();
                Map<String, Object> requestMap = facesContext
                    .getExternalContext().getRequestMap();
                NavigationHandler navigationHandler = facesContext
                    .getApplication().getNavigationHandler();
                try {
                    // Push some useful stuff to the request scope for use in
                    // the page
                    requestMap.put("currentViewId", vee.getViewId());
                    navigationHandler.handleNavigation(facesContext, null,
                        "/index");
                    facesContext.renderResponse();
                } finally {
                    i.remove();
                }
            }
        }
        // At this point, the queue will not contain any ViewExpiredEvents.
        // Therefore, let the parent handle them.
        getWrapped().handle();
    }
}

在faces-config.xml中添加以下行:

代码语言:javascript
复制
<factory>
    <exception-handler-factory>path.to.class.ViewExpiredExceptionExceptionHandlerFactory</exception-handler-factory>

就这样,你可以走了。

编辑

好的,我忘记了Omnifaces的方法,我不使用它,因为一旦设置了错误页面,它就会警告您为404创建页面,以及另一个我现在不记得的页面。应该将以下内容添加到faces-config.xml中:

代码语言:javascript
复制
<factory>
<exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>

你的密码就能用了。欲了解更多信息,请参见FullAjaxExceptionHandlerFactory的展示版,来自无所不在的http://showcase.omnifaces.org/exceptionhandlers/FullAjaxExceptionHandler;jsessionid=sOwX0FVqcY-InUhvDWEMksfQ网站。

不要忘记将所需的jar添加到项目中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21800128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档