【问题标题】:Primefaces ExceptionHandler not working as in the showcasePrimefaces ExceptionHandler 不像在展示中那样工作
【发布时间】:2015-05-09 16:45:56
【问题描述】:

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

【问题讨论】:

    标签: ajax jsf jsf-2 primefaces exception-handling


    【解决方案1】:

    再次打开您提供的展示链接。现在单击 文档 选项卡。您会看到:

    11.3 异常处理程序

    PrimeFaces 提供了一个内置的异常处理程序来处理 ajax 和非 ajax 中的异常 轻松请求。

    配置

    faces配置文件中需要配置ExceptionHandler和ElResolver。

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

    采取相应措施。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      相关资源
      最近更新 更多