如何将现有的JSF组件添加到我自己的自定义组件中?

Chr*_*ale 5 jsf taglib uicomponents

我有一个扩展UIComponent和UIOutput的标记类.在这个类中我有encodeBegin和encodeEnd,我可以使用我的contextWriter通过使用writer.startElement("div",myComponent)等输出我想要的任何类型的html标签.

我现在的问题是我需要插入例如一个而不是使用writer.startElement.我可以通过执行getChildren()来完成此操作.add(HtmlCommandButton button = new HtmlCommandButton()); 但是当我这样做时,我似乎无法输出我希望它们出现的组件,就像我可以使用write.startElement.

有没有人能在我自己的taglibrary中如何利用richfaces标签,JSF标签和类似的东西?简而言之,我真正想做的是在我的encodeBegin中:

writer.startElement("a4j:commandButton", myComponent);
writer.writeAttribite("action", "#{Handler.myAction}", null);
writer.endElement("a4j:commandButton");
Run Code Online (Sandbox Code Playgroud)

谢谢你提前

McD*_*ell 3

您不能随心所欲地使用ResponseWriter 。我可以想到如何以编程方式添加子控件的两种方法是通过绑定属性(请参阅此答案)或在通常创建控件的位置(在 JSP 中,即在标记类中)。

JSF 组件可以通过两种方式包含其他控件:作为子控件或作为命名方面。组件始终控制它们渲染面的方式;如果他们要渲染他们的孩子,他们必须为getRendersChildren返回 true 。

这是未经测试的代码,但顺序如下:

  @Override
  public boolean getRendersChildren() {
    return true;
  }

  @Override
  public void encodeBegin(FacesContext context)
      throws IOException {
    // should really delegate to a renderer, but this is only demo code
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("span", this);
    String styleClass = getStyleClass();
    writer
        .writeAttribute("class", styleClass, "styleClass");

    UIComponent headerComponent = getFacet("header");
    if (headerComponent != null) {
      headerComponent.encodeAll(context);
    }

    writer.startElement("hr", null);
  }

  @Override
  public void encodeChildren(FacesContext context)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    for (UIComponent kid : getChildren()) {
      kid.encodeAll(context);
      writer.startElement("br", null);
    }
  }

  @Override
  public void encodeEnd(FacesContext context)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    writer.endElement("span");
  }
Run Code Online (Sandbox Code Playgroud)