Search code examples
jsfbuttonprogrammatically-created

How to create a <h:button> dynamically


How to create a <h:button> from Java? I can create a <h:commandButton> like this:

HtmlCommandButton button = new HtmlCommandButton();

But I can't find the Java class for the <h:button>. For example I need to create this tag from Java:

<h:button outcome="test.xhtml" />

How can I achieve it?


Solution

  • Just peek around in javax.faces.component.html package for all of them.

    The <h:button> is represented by the HtmlOutcomeTargetButton class.

    HtmlOutcomeTargetButton button = new HtmlOutcomeTargetButton();
    button.setOutcome("test.xhtml");
    

    Said that, using XHTML to define the component tree will end up in much better maintainable code. See also How to create dynamic JSF form fields and JSTL in JSF2 Facelets... makes sense?