Primefaces ExceptionHandler 无法像展示中那样工作

Primefaces ExceptionHandler not working as in the showcase

我已经在 primefaces 中尝试了新的 ExceptionHandler 标签,但我似乎遗漏了这个标签的一些东西。

基本上我的代码与 http://www.primefaces.org/showcase/ui/misc/exceptionHandler.xhtml 中的代码相同,问题是添加此标记不会改变任何内容我在客户端没有得到任何东西。 标准 js API 不适用于主要按钮:

jsf.ajax.addOnError(function(data) {alert('Error');})

我注意到我的 运行 应用程序和 Primefaces 展示柜之间的唯一区别是我得到的 ajax 响应与展示柜的不同:

我的回复:

展示响应

这是代码

<h:form>
<h3 style="margin-top:0">AJAX</h3>
<p:commandButton actionListener="#{exceptionHandlerView.throwViewExpiredException}"
                 ajax="true"
                 value="Throw ViewExpiredException!" />
<p:commandButton actionListener="#{exceptionHandlerView.throwNullPointerException}"
                 ajax="true"
                 value="Throw NullPointerException!" />
<p:commandButton actionListener="#{exceptionHandlerView.throwWrappedIllegalStateException}"
                 ajax="true"
                 value="Throw IllegalStateException!" />

<h3>Non-AJAX</h3>
<p:commandButton actionListener="#{exceptionHandlerView.throwViewExpiredException}"
                 ajax="false"
                 value="Throw ViewExpiredException!" />
<p:commandButton actionListener="#{exceptionHandlerView.throwNullPointerException}"
                 ajax="false"
                 value="Throw NullPointerException!" />


<p:ajaxExceptionHandler type="javax.faces.application.ViewExpiredException"
                        update="exceptionDialog"
                        onexception="PF('exceptionDialog').show();" />

<p:ajaxExceptionHandler type="java.lang.NullPointerException"
                        update="exceptionDialog"
                        onexception="PF('exceptionDialog').show();" />

<p:dialog id="exceptionDialog" header="Exception '#{pfExceptionHandler.type}' occured!" widgetVar="exceptionDialog"
          height="500px">
    Message: #{pfExceptionHandler.message} <br/>
    StackTrace: <h:outputText value="#{pfExceptionHandler.formattedStackTrace}" escape="false" /> <br />

    <p:button onclick="document.location.href = document.location.href;"
              value="Reload!"
              rendered="#{pfExceptionHandler.type == 'javax.faces.application.ViewExpiredException'}" />
</p:dialog>

package org.primefaces.showcase.view.misc;


import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class ExceptionHandlerView {

    public void throwNullPointerException() {
        throw new NullPointerException("A NullPointerException!");
    }

    public void throwWrappedIllegalStateException() {
        Throwable t = new IllegalStateException("A wrapped IllegalStateException!");
        throw new FacesException(t);
    }

    public void throwViewExpiredException() {
        throw new ViewExpiredException("A ViewExpiredException!",
                FacesContext.getCurrentInstance().getViewRoot().getViewId());
    }
}

再次打开您给定的展示柜 link。现在单击 文档 选项卡。您会看到:

11.3 Exception Handler

PrimeFaces provides a built-in exception handler to take care of exceptions in ajax and non-ajax requests easily.

Configuration

ExceptionHandler and an ElResolver configured is required in faces configuration file.

<application>
     <el-resolver>
         org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
    </el-resolver>
</application>
<factory>
    <exception-handler-factory>
       org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
    </exception-handler-factory>
</factory>

据此采取行动。