当嵌套在ui:repeat中时,具有自定义后备组件的复合组件会奇怪地中断

时间:2012-11-19 16:24:57

标签: jsf-2 composite-component

我的问题很简单,我有一个指定了componentType的复合组件。当我在页面上单独使用复合组件时,它工作正常。当我将它嵌入另一个复合组件(也在ui:repeat中)时,我得到一个异常,试图找到我在UINamingContainer中的componentType类上定义的属性 - 这是一个错误的类。我发现我可以用cc.whatever替换component.parent.parent.parent.whatever来解决这个问题,但我发现这有点严重。我假设我做了一些让JSF感到困惑的蠢事,但我不确定它是什么或者我应该做些什么不同。

现在,在令人难以忍受的细节中,我有以下复合组件:

<?xml version='1.0' encoding='UTF-8' ?>
<ui:component xmlns:ui="http://java.sun.com/jsf/facelets"
              xmlns:cc="http://java.sun.com/jsf/composite"
              xmlns:ice="http://www.icesoft.com/icefaces/component"
              xmlns:f="http://java.sun.com/jsf/core">
  <cc:interface componentType="edu.nrao.sss.tools.resrccat.ui.support.LinearVelocityUIComponent">
    <cc:attribute name             = "value"
                  class            = "edu.nrao.sss.measure.LinearVelocity"
                  required         = "true"
                  shortDescription = "The LinearVelocity value to show or edit."/>

    <cc:attribute name             = "separate-units"
                  type             = "boolean"
                  default          = "true"
                  shortDescription = "If true, provide a drop-box to edit the units separately from the value."/>
  </cc:interface>
  <cc:implementation>
    <ice:inputText id="velocity" value="#{cc.attrs.value}" rendered="#{not cc.attrs['separate-units']}"/>

    <ui:fragment rendered="#{cc.attrs['separate-units']}">
      <ice:inputText id="velocityDecimal" value="#{cc.velocityDecimal}"/>
      <ice:selectOneMenu id="velocityUnits" value="#{cc.velocityUnits}">
        <f:selectItems value="#{siFactory.linearVelocityUnits}"/>
      </ice:selectOneMenu>
    </ui:fragment>
  </cc:implementation>
</ui:component>

支持组件是:

@FacesComponent("edu.nrao.sss.tools.resrccat.ui.support.LinearVelocityUIComponent")
public class LinearVelocityUIComponent extends UIInput implements NamingContainer {

  @Override public String getFamily() {
    return UINamingContainer.COMPONENT_FAMILY;
  }

  public LinearVelocity getVelocity() {
    return (LinearVelocity) getValueExpression("value").getValue(FacesContext.getCurrentInstance().getELContext());
  }

  public void setVelocity(LinearVelocity newVelocity) {
    getValueExpression("value").setValue(FacesContext.getCurrentInstance().getELContext(), newVelocity);
  }

  public BigDecimal getVelocityDecimal() {
    return getVelocity().getValue();
  }

  public LinearVelocityUnits getVelocityUnits() {
    return getVelocity().getUnits();
  }

  public void setVelocityDecimal(BigDecimal newVelocity) {
    setVelocity(new LinearVelocity(newVelocity, getVelocityUnits()));
  }

  public void setVelocityUnits(LinearVelocityUnits units) {
    setVelocity(new LinearVelocity(getVelocityDecimal(), units));
  }
}

当我在页面上单独使用此组件时,一切都很好:

<p>Your linear velocity is <rct:linear-velocity value="#{test.velocity}"/></p>

当我将它嵌入另一个复合组件时,如下所示:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:component xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ice="http://www.icesoft.com/icefaces/component"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:rct="http://java.sun.com/jsf/composite/components/rct">
  <cc:interface>
    <cc:attribute name="specs"        required="true"/>
    <cc:attribute name="type"         default="line"/>
  </cc:interface>
  <cc:implementation>
    <table class="iceDatTbl">
      <thead>
        <tr>
          ...
          <th class="iceDatTblColHdr2">Minimum<br/>Span</th> 
          ...
        </tr>
      </thead>
      <tbody>
        <ui:repeat var="spec" value="#{cc.attrs.specs}" varStatus="stat">
          <tr class="#{stat.even ? 'iceDatTblRow1' : 'iceDatTblRow2'}">
            ...
            <td class="iceDatTblCol2">
              Req: <rct:linear-velocity value="#{spec.minimumSpan}"/><br/>
              Sug: #{spec.suggestedMinimumSpan}
            </td>
            ...
          </tr>
        </ui:repeat>
      </tbody>
    </table>
  </cc:implementation>
</ui:component>

我得到了这个例外:

Nov 19, 2012 4:10:37 PM org.icefaces.impl.application.ExtendedExceptionHandler handle
WARNING: queued exception
javax.el.PropertyNotFoundException: /resources/components/rct/linear-velocity.xhtml @20,113 value="#{cc.velocityDecimal}": Property 'velocityDecimal' not found on type javax.faces.component.UINamingContainer
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:111)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at com.sun.faces.facelets.component.UIRepeat$SavedState.populate(UIRepeat.java:823)
    at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:369)
    at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:375)
    at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:375)
    at com.sun.faces.facelets.component.UIRepeat.saveChildState(UIRepeat.java:355)
    at com.sun.faces.facelets.component.UIRepeat.setIndex(UIRepeat.java:440)
    at com.sun.faces.facelets.component.UIRepeat.visitTree(UIRepeat.java:613)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UINamingContainer.visitTree(UINamingContainer.java:163)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at org.icefaces.impl.component.UISeriesBase.visitRows(UISeriesBase.java:1174)
    at org.icefaces.impl.component.UISeriesBase.visitTree(UISeriesBase.java:1065)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIForm.visitTree(UIForm.java:344)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
    at org.icefaces.impl.event.RestoreResourceDependencies.processEvent(RestoreResourceDependencies.java:24)
    at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
    at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2168)
    at com.sun.faces.application.ApplicationImpl.invokeListenersFor(ApplicationImpl.java:2144)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:302)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:246)
    at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:812)
    at javax.faces.component.UIViewRoot.encodeBegin(UIViewRoot.java:962)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at com.ocpsoft.pretty.faces.application.PrettyViewHandler.renderView(PrettyViewHandler.java:163)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    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:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:137)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at edu.nrao.sss.webapp.LookupUserFilter.doFilter(LookupUserFilter.java:57)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.jasig.cas.client.util.HttpServletRequestWrapperFilter.doFilter(HttpServletRequestWrapperFilter.java:50)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.jasig.cas.client.authentication.AuthenticationFilter.doFilter(AuthenticationFilter.java:111)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

我可以做的替代使它能够发挥作用:

<ui:fragment rendered="#{cc.attrs['separate-units']}">
  <ice:inputText id="velocityDecimal" value="#{component.parent.parent.parent.velocityDecimal}"/>
  <ice:selectOneMenu id="velocityUnits" value="#{component.parent.parent.parent.velocityUnits}">
    <f:selectItems value="#{siFactory.linearVelocityUnits}"/>
  </ice:selectOneMenu>
</ui:fragment>

这种感觉确实是错误的并且在历史上每当我试图通过像这样的黑客攻击JSF几个月后它就会毁了我的生命。我非常感谢任何帮助!我喜欢JSF,我非常喜欢使用复合组件来构建东西,但我真的需要能够嵌套复合组件。

编辑:我发现在Glassfish中,该组件根本无法独立呈现。它产生相同的错误(UINamingContainer没有将此属性设置为LinearVelocityUIComponent)嵌套在另一个组件中。

2 个答案:

答案 0 :(得分:2)

我能够在Tomcat 7.0.30上使用Mojarra 2.1.14重现您的问题(并使用那些ICEfaces组件的标准JSF <h:xxx>等价物)。这归咎于Mojarra中嵌套<ui:repeat>的破坏状态保存机制。老实说,我不确定"nested ui:repeat"上哪一个关于{{3}}的Mojarra bug报告涵盖了您的特定问题,报告太多,而您的特定报告对我来说是新的。

在MyFaces 2.1.9中,完全相同的代码对我来说没问题。当我至少用<ui:repeat><h:dataTable>替换外<p:dataList>时,它也可以正常工作。

所以你有两个选择来解决这个问题:

  1. 替换(至少外部)<ui:repeat>一个完整的UIData实现,例如<h:dataTable><p:dataList>或任何ICEfaces相当于我不能从头顶告诉。这可能不会生成您想要的HTML,但它可以使用CSS,您可以做很多事情。

  2. 用MyFaces取代Mojarra。不要忘记彻底(单元)测试整个webapp。 MyFaces也有自己的一系列怪癖和错误。

答案 1 :(得分:1)

如果可以帮助任何人,我解决了一个类似的问题,即我将一个复合组件(cc)嵌套在另一个复合组件中。一个实例可以,一个页面中的两个实例有问题,试图找到属于嵌套抄送的类字段,但是正在宿主抄送中寻找它们。问题是我为两个抄送使用了相同的名称变量。因此,EL表达式“ cc.attrs.bean”对于JSF来说是令人困惑的...我通过在CC之间保留不同的参数名称来解决了它。花了我一段时间才能找到答案。