I've been trying to create a custom ui component for jsf to replace <f:selectItems />
, which is wrapped in a selectOneMenu. So my facesComponent needs to generate all the options while the selectOneMenu provides the <select></select>
.
Problem is that the options are not rendered inside the selectOneMenu, but rather just outside of it.
My facesComponent looks like this:
@FacesComponent(value = "be.mokuril.jsf.SelectItemsForEnum")
public class SelectItemsForEnum extends UISelectItems {
@Override
public void encodeAll(FacesContext facesContext) throws IOException {
ResponseWriter responseWriter = ResponsefacesContext.getResponseWriter();
responseWriter.startElement("option", null);
responseWriter.writeAttribute("value", 1, null);
responseWriter.write("option1");
responseWriter.endElement("option");
responseWriter.startElement("option", null);
responseWriter.writeAttribute("value", 2, null);
responseWriter.write("option2");
responseWriter.endElement("option");
}
And this is my taglib:
<facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0" id="mw"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
>
<namespace>http://www.mokuril.be/jsf/mw</namespace>
<composite-library-name>mw</composite-library-name>
<tag>
<tag-name>selectItemsForEnum</tag-name>
<component>
<component-type>be.mokuril.jsf.SelectItemsForEnum</component-type>
</component>
</tag>
And the xhtml to reproduce the problem:
<h:form>
<h:selectOneMenu>
<mw:selectItemsForEnum />
</h:selectOneMenu>
</h:form>
I've also been looking at the component tree:
<HtmlSelectOneMenu disabled="false" id="j_idt7" immediate="false" inView="true" localValueSet="false" readonly="false" rendered="true" required="false" transient="false" valid="true">
<SelectItemsForEnum id="j_idt8" inView="true" rendered="true" transient="false"/>
</HtmlSelectOneMenu>
And if I use <f:selectItems />
instead of my component I get this:
<UISelectItems id="j_idt9" inView="true" rendered="true" transient="false"/>
Which is actually what I expected it to look like, but I clearly must be overlooking something important.