Skip to main content
 首页 » 编程设计

ajax之Primefaces ExceptionHandler不能像展示中那样工作

2025年12月25日32me-sa

我已经在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()); 
    } 
} 

请您参考如下方法:

再次打开您给定的展示链接。现在,单击“文档”选项卡。您将看到:

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> 


按指示行动。