JSF ResponseWriter Custom Components

I know about the startElement, endElement and writeAttribute methods on ResponseWriter. My problem is that I want, for example, to output h: commandLink, declaring it as HtmlCommandLink link = new HtmlCommandLink();.

How can I output other UIComponents like this in my own component? I might want to use some ajax RichFaces components in my components, so I hope that I can avoid all the problems.

Edit: what I'm trying to do is create my own tag library with the following tag <myTags:commentTree>. Each comment has an answer button when the answer button is pressed. I present the response form under the comment. As soon as this is shown, I would like to output, for example, the richfaces component <a4j:commandButton>. This should be done inside my own java tag file, which Ive called for CommentsTreeUI.java.

I usually output all my elements that display forms and buttons with writer.startElement("input", myComponent); writer.writeAttribute("type", "button", null);, but if I could do it instead, for example startElement("a4j:commandbutton", myComponent), it would help my ALOT, since it has all the built-in ajax functions, etc.

Any clues?

+3
source share
3 answers

HtmlCommandButton button = new HtmlCommandButton();
button.encodeAll(context);
+2

- :

HtmlCommandLink link = new HtmlCommandLink();
getChildren().add(link);

, , , , HTML (, HTML-), - .

+1

One approach to creating composite controls is to use a binding attribute to associate a tag with your own code:

<f:view>
    <h:form>
        <h:panelGroup binding="#{compositeControlBean.panelGrid}" />
    </h:form>
</f:view>

Bean configuration in faces-config.xml:

<managed-bean>
    <managed-bean-name>compositeControlBean</managed-bean-name>
    <managed-bean-class>
        composite.CompositeControlBean
    </managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

Bean code:

/**
 * Configure this bean in request scope as "compositeControlBean".
 */
public class CompositeControlBean {

  private transient UIComponent panelGrid;

  public UIComponent getPanelGrid() {
    if (panelGrid == null) {
      panelGrid = createCompositePanel();
    }
    return panelGrid;
  }

  public void setPanelGrid(UIComponent panelGrid) {
    this.panelGrid = panelGrid;
  }

  private UIComponent createCompositePanel() {
    initContextMemebers();

    UIComponent commandLink = createCommandLink();

    String id = view.createUniqueId();
    UIComponent panelGrid = application
        .createComponent("javax.faces.HtmlPanelGroup");
    panelGrid.setId(id);
    panelGrid.setRendererType("javax.faces.Group");

    panelGrid.getChildren().add(commandLink);

    return panelGrid;
  }

  private UIComponent createCommandLink() {
    // create control
    String id = view.createUniqueId();
    UIComponent commandLink = application
        .createComponent("javax.faces.HtmlCommandLink");
    commandLink.setId(id);
    commandLink.setRendererType("javax.faces.Link");
    // set attributes (bind to printHello method)
    Map<String, Object> attributes = commandLink
        .getAttributes();
    MethodExpression action = expressionFactory
        .createMethodExpression(elContext,
            "#{compositeControlBean.printHello}",
            String.class, new Class<?>[0]);
    attributes.put("value", "print hello");
    attributes.put("actionExpression", action);
    return commandLink;
  }

  private transient FacesContext context;
  private transient Application application;
  private transient ELContext elContext;
  private transient ExpressionFactory expressionFactory;
  private transient UIViewRoot view;

  private void initContextMemebers() {
    context = FacesContext.getCurrentInstance();
    application = context.getApplication();
    elContext = context.getELContext();
    expressionFactory = application.getExpressionFactory();
    view = context.getViewRoot();
  }

  public String printHello() {
    System.out.println("Hello");
    return null;
  }

}
0
source

All Articles