Primefaces ajaxExceptionHandler在liferay 7中不起作用

时间:2017-04-25 13:02:27

标签: jsf primefaces liferay jsf-2.2 liferay-7

Primefaces 6.1 ajaxExceptionHandler在liferay 7 portlet中没有按预期工作。 基于Primefaces用户指南,我尝试实现简单的异常处理。按下commandButton时,支持bean会抛出NullPointerException,它应该显示在对话框窗口中。问题是,当弹出对话框时,不会显示异常信息。似乎返回的ajax响应本身包含异常信息(如下所示),但对话框组件未相应更新。

test.xhtml(摘录)

<h:body>

    <h:form>
        <h3 style="margin-top:0">AJAX 1410</h3>
        <p:commandButton actionListener="#{policyAdminBean.throwNpe}"
                         ajax="true"
                         value="Throw NullPointerException!" />

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

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

    <p:dialog id="exceptionDialog" header="Exception '#{pfExceptionHandler.type}' occured!" widgetVar="exceptionDialogVar"
              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>


</h:body>

TestBean.java

@Named
@ViewScoped
public class TestBean implements Serializable {
    private static final long serialVersionUID = -4856350663999482370L;

    public void throwNpe(){
        throw new NullPointerException("test exception");
    }
}

faces-config.xml(摘录)

<application>
        <message-bundle>Language</message-bundle>
        <locale-config>
            <default-locale>hu</default-locale>
        </locale-config>
        <el-resolver>
            org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
        </el-resolver>
    </application>
    <lifecycle>
        <phase-listener>com.liferay.faces.util.lifecycle.DebugPhaseListener</phase-listener>
    </lifecycle>
    <factory>
        <exception-handler-factory>
            org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
        </exception-handler-factory>
    </factory>

ajax响应(摘录)

<partial-response id="_policyadmin_WAR_wfsadminportlets_">
    <update id="_policyadmin_WAR_wfsadminportlets_:exceptionDialog"><![CDATA[
        <div id="_policyadmin_WAR_wfsadminportlets_:exceptionDialog"
             class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-shadow ui-hidden-container">
            <div class="ui-dialog-titlebar ui-widget-header ui-helper-clearfix ui-corner-top"><span
                    id="_policyadmin_WAR_wfsadminportlets_:exceptionDialog_title" class="ui-dialog-title">Exception 'java.lang.NullPointerException' occured!</span><a
                    href="#" class="ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all"
                    aria-label="Close"><span class="ui-icon ui-icon-closethick"></span></a></div>
            <div class="ui-dialog-content ui-widget-content">
                Message: test exception <br/>
                StackTrace: java.lang.NullPointerException: test exception<br/> at

                [removed for brevity...]

                java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)<br/> at
                java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)<br/> at
                java.lang.Thread.run(Thread.java:745)<br/> <br/></div>
        </div>
        <script id="_policyadmin_WAR_wfsadminportlets_:exceptionDialog_s" type="text/javascript">$(function () {
            PrimeFaces.cw("Dialog", "exceptionDialogVar", {
                id: "_policyadmin_WAR_wfsadminportlets_:exceptionDialog",
                height: "500px"
            });
        });</script>
        ]]>
    </update>
    <eval><![CDATA[var
        hf=function(type,message,timestampp){PF('exceptionDialogVar').show();};hf.call(this,"java.lang.NullPointerException","test
        exception","2017-04-25 14:54:55");]]>
    </eval>
</partial-response>

1 个答案:

答案 0 :(得分:1)

好问题!

PrimeFaces AJAX异常处理程序的这种使用模式在版本6.0之前的PrimeFaces中正在使用。所以如果你回复说PrimeFaces 5.3,你会看到它在Liferay 7上运行。破坏PrimeFaces的提交是e22e40a,这是一个复杂的提交,通过改变PrimeFaces部分的方式在不知不觉中影响portlet回应是建立的。

pull request已发送到PrimeFaces integrators以解决此问题。 https://github.com/primefaces/primefaces/pull/2333

合并此拉取请求后,您可以build PrimeFaces from the latest source,并且您会看到这是固定的。

详情: 具体来说,提交e22e40a改变了PrimePartialResponseWriter.startDocument()方法eliminating its call to encodeCallbackParams(),而后者又调用了startChangesIfNecessary() method in mojarra。由于这些电话被淘汰,所以没有“改变”#34;元素被引入到部分响应中。如果没有更改,则对话框DOM中不会进行更新。这是一个工作堆栈,显示e22e40a提交之前的calls down into startChangesIfNecessary