Quick way to create JSF custom component

眉间皱痕 提交于 2019-12-05 21:08:17

You can do a limited amount of templating using (for example) jsp:include and f:subview.

Alternatively, you can extend a UIComponent overriding selected methods and then provide it via an existing tag and a managed bean using the binding attribute. This still requires a reasonably detailed understanding of component development (and the consequences of this choice), but could cut down the number of files/volume of code significantly.

This approach is a bit of a hack, but might be OK for short-term stuff. You wouldn't do it for component libraries you want to distribute or components requiring long term maintenance.

The new component:

public class QuickComponent extends HtmlOutputText {
  @Override public void encodeAll(FacesContext context) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    writer.writeText("I'm not really a HtmlOutputText", null);
    for (UIComponent kid : getChildren()) {
      if (kid instanceof UIParameter) {
        UIParameter param = (UIParameter) kid;
        writer.startElement("br", this);
        writer.endElement("br");
        writer.writeText(param.getName() + "=" + param.getValue(), null);
      }
    }
  }
}

The bean providing an instance:

/**Request-scope managed bean defined in faces-config.xml*/
public class QuickComponentProviderBean {
  private QuickComponent quick;

  public void setQuick(QuickComponent quick) {
    this.quick = quick;
  }

  public QuickComponent getQuick() {
    if (quick == null) {
      quick = new QuickComponent();
    }
    return quick;
  }
}

Note: don't reuse a single bean property for multiple tags in your views, or they'll reference the same object instance.

Adding the new component to the view:

<h:outputText binding="#{quickComponentProviderBean.quick}">
  <f:param name="Hello" value="World" />
</h:outputText>

Note: the attributes that can be defined have not changed. They're fixed by the TLD.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!