htmlformsjakarta-eejsf-2form-generator

Programmatically create HTML form fieldset tag with JSF


In my Java code I want to programmatically create a <fieldset> tag that I can use in my JSF form.

The setup of my form looks like this:

Application app = FacesContext.getCurrentInstance().getApplication();

HtmlForm form = (HtmlForm) app.createComponent(HtmlForm.COMPONENT_TYPE);
form.setStyleClass("pure-form pure-form-stacked");

As you can see I use HtmlForm.COMPONENT_TYPE as an identifier for the JSF UI component but I haven't found an identifier for a fieldset so I tried:

UIComponent fieldset = app.createComponent("fieldset");
form.getChildren().add(fieldset);

Unfortunately this is not working so I have to come up with another solution. Do you have any ideas?

Is there a general approach how HTML tags (which are unknown in the JSF context) can be created?


Solution

  • You can try the following:

    Theres a component called <f:verbatim> which you would use in xhtml like this:

    <f:verbatim escape="false">
        <fieldset id="blah"></fieldset>
    </f:verbatim>
    

    To achieve that programmaticlly you can add this component like this:

    String fieldsetHTMLText ="<fieldset id=\"blah\"></fieldset>";
    
    UIOutput verbatim = new UIOutput();
    verbatim.setRendererType("javax.faces.Text");
    verbatim.getAttributes().put("escape", false);
    verbatim.setValue(fieldsetHTMLText);