复合组件的Ajax侦听器方法javax.faces.FacesException:无法在使用页面的顶级组件中查找属性

时间:2013-11-27 10:06:18

标签: ajax jsf-2 composite-component

我正在尝试向复合组件添加Ajax侦听器,但是我得到以下异常:

09:40:54,161 ERROR [[Faces Servlet]] Servlet.service() for servlet Faces Servlet threw exception
javax.faces.FacesException: : Unable to find attribute with name "myListener" in top level component in consuming page,  or with default value in composite component.  Page author or composite component author error.
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.retargetMethodExpressions(FaceletViewHandlingStrategy.java:718)
    at com.sun.faces.facelets.tag.jsf.CompositeComponentTagHandler.applyNextHandler(CompositeComponentTagHandler.java:200)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.tag.ui.DefineHandler.applyDefinition(DefineHandler.java:106)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:178)
    at com.sun.faces.facelets.impl.DefaultFaceletContext$TemplateManager.apply(DefaultFaceletContext.java:395)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeDefinition(DefaultFaceletContext.java:366)
    at com.sun.faces.facelets.tag.ui.InsertHandler.apply(InsertHandler.java:111)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
    at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:187)
    at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
    at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:320)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:379)
    at com.sun.faces.facelets.impl.DefaultFacelet.include(DefaultFacelet.java:358)
    at com.sun.faces.facelets.impl.DefaultFaceletContext.includeFacelet(DefaultFaceletContext.java:199)
    at com.sun.faces.facelets.tag.ui.CompositionHandler.apply(CompositionHandler.java:155)
    at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
    at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:87)
    at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:164)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:914)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at dk.jyskebank.jee.servlet.filter.LogCorrelationIdFilter.doFilter(LogCorrelationIdFilter.java:28)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:183)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:534)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:95)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.jboss.web.tomcat.service.sso.ClusteredSingleSignOn.invoke(ClusteredSingleSignOn.java:731)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.internalProcess(ActiveRequestResponseCacheValve.java:74)
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:47)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:905)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:613)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2036)
    at java.lang.Thread.run(Unknown Source)

我的复合组件:

<cc:interface>
    <cc:attribute name="text" required="true" />
    <cc:attribute name="renderAsLink" required="true" />
    <cc:attribute name="year" required="true" />
    <cc:attribute name="month" required="true" />
    <cc:attribute name="fravaersoplysningCode" required="true" />
    <cc:attribute name="noteDialog" required="true" />
    <cc:attribute name="myListener"  method-signature="void listener()" required="true" />
</cc:interface>

<cc:implementation> 
    <p:commandLink rendered="#{cc.attrs.renderAsLink}">
        <p:ajax oncomplete="#{cc.attrs.noteDialog}.show()" listener="#{cc.attrs.myListener}"/>

    </p:commandLink>
</cc:implementation>

我的控制器方法:

public void testListener(){
    logger.debug("testListener : {}", String.format("Im working"));
}

我的xhtml网站:

            <p:column headerText="#{monthEnumBundle[fravaersoplysningerView.fravaerOversigt.months.get(12)]}">
                <component:fravaersoversigtLink 
                    text="#{fravaersoplysning.fravaerMonth.get(12).amount}"
                    renderAsLink="#{fravaersoplysning.fravaerMonth.get(12).content}" 
                    year="#{fravaersoplysning.fravaerMonth.get(12).year}"
                    month="#{fravaersoplysning.fravaerMonth.get(12).month}"
                    fravaersoplysningCode="#{fravaersoplysning.fravaersoplysningCode}"
                    noteDialog="detailDialog"
                    myListener="#{fravaersoplysningerController.testListener()}"/>

            </p:column>

似乎myListener的绑定(myListener =“#{fravaersoplysningerController.testListener())被解析为null。如果我尝试删除”method-signature =“void listener()”“我不会得到异常不再,但当然我失去了所需的功能。

如果我尝试将#{fravaersoplysningerController.testListener()方法绑定到我的xhtml网站上的一个按钮,那么该方法就会被调用而没有任何问题。

任何想法都错了吗?

我正在竞选: JSF 2.1.26, JBOSS 5.1, Primefaces 4.0.4, EL 2.0.2

2 个答案:

答案 0 :(得分:2)

好的,这个问题的解决方案实际上非常简单。我做了一个测试用例,当然一切正常,这种情况更令人沮丧。

我注意到第一件事就是将required属性设置为true,导致一切正常。

经过几个小时的测试后,我的配偶注意到我们也在xhtml网站上的另一个地方使用复合组件(在深黑处的某处:P)。 但是我们没有在那里分配监听器属性。因此,当然因为该属性是必需的并且未设置,所以会出现异常

答案 1 :(得分:1)

您是否尝试删除侦听器调用中的括号?

之前:

myListener="#{fravaersoplysningerController.testListener()}"

后:

myListener="#{fravaersoplysningerController.testListener}"

另外,想知道'f:ajax'将'oncomplete'作为一个属性,会产生类似的预期

<f:ajax 
    onevent="function(data){if (data=="complete) { #{cc.attrs.noteDialog}.show();}" 
/>

或者像@BalusC所说的那样,ajax的主要代表......

编辑:刚刚在IDE中破解了您的示例,缩小了一点:

XHTML:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:io="http://java.sun.com/jsf/composite/inout"
    ...
/>
...
<h:body>

<h:dataTable var="item" value="#{scPaymentLevelValues}">
    <h:column>
        <io:testSomething
            text="myText"
            renderAsLink="myRenderAsLink"
            year="2012"
            month="12"
            fravaersoplysningCode="womething"
            noteDialog="yeah"
            myListener="#{testBean.testListener}" />
    </h:column>
</h:dataTable>
</h:body>
</html>

成分:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:cc="http://java.sun.com/jsf/composite">

    <cc:interface>
    <cc:attribute name="text" required="true" />
        <cc:attribute name="renderAsLink" required="true" />
        <cc:attribute name="year" required="true" />
        <cc:attribute name="month" required="true" />
        <cc:attribute name="fravaersoplysningCode" required="true" />
        <cc:attribute name="noteDialog" required="true" />
        <cc:attribute name="myListener" 
                      method-signature="void listener()" required="true" />          
    </cc:interface>

    <!-- IMPLEMENTATION -->
    <cc:implementation> 
     <h:commandButton 
                 value="hhii"
                 rendered="true">
                 <f:ajax 
                     oncomplete="alert('hi');" 
                     listener="#{cc.attrs.myListener}" />
             </h:commandButton> 
    </cc:implementation>
</ui:composition>

豆:

import java.io.Serializable;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("testBean")
@Scope(ScopeType.CONVERSATION)
public class TestBean implements Serializable {

    public void testListener(){
        System.out.println("testListener : {}"+ String.format("Im working"));
    }
}

获得所需的输出testListener : {}Im working。使用Mojarra 2.1.7,Seam 2.3.1.CR1。您是否可以尝试降级到h:dataTables和h:commandButton,检查它是否在那里工作......如果这样升级到primefaces直到它停止工作 - 然后决定如何继续?。

希望,这有帮助...如果是,请投票。 : - )