java - Create dynamic ENUM Menu Tag in JSF 1.2 -


i'm struggling on problem because destroys modularity bit. i'm using jsf 1.2 , facing use h:selectonemenu select 1 value of given set of enums.

e.g.

public enum state {             a,b,c; }; 

for instance, i'd sth. this:

<h:selectonemenu>       <f:selectitem itemvalue="a" itemlabel="text" />       <f:selectitem itemvalue="b" itemlabel="text" />       <f:selectitem itemvalue="c" itemlabel="text" /> </h:selectonemenu> 

so i'm searching way (e.g. custom tag) more generic.

regarding view component want available choices independent f:selectitem in menu.

the available choices passed list or sth. else.

the first method tried use a4j:repeat tag selectitems set of choices passed tag, thing got empty menu.

my idea kind of custom tag looks this:

<namespace:enummenu enumvalues="#{values}" value=#{value}" /> 

consider passed set of enums values a,b,c,d should result in sth. like

    <h:selectonemenu value=#{value}>           <!-- repeat every enum item in passed #values -->           <f:selectitem itemvalue="a" />           <f:selectitem itemvalue="b" />           <f:selectitem itemvalue="c" />           <f:selectitem itemvalue="d" />     </h:selectonemenu> 

every appreciated

the <a4j:repeat> failed because <f:xxx> components must available during view build time in order inserted in component tree, <a4j:repeat> runs during view render time. need jstl <c:foreach> instead.

<h:selectonemenu ...>     <c:foreach ...>         <f:selectitem />     </c:foreach> </h:selectonemenu> 

see also:


an alternative use <f:selectitems>. jsf 1.2 has builtin enum converter, need this:

private state selectedstate; // +getter+setter private selectitem[] availablestateitems; // +getter (no setter necessary!)  @postconstruct public void init() {     state[] availablestates = state.values();     availablestateitems = new selectitem[availablestates.length];      (int = 0; < availablestates.length; i++) {         availablestateitems[i] = new selectitem(availablestates[i]);     } } 

with

<h:selectonemenu value="#{bean.selectedstate}">     <f:selectitems value="#{bean.availablestateitems}" /> </h:selectonemenu> 

you can put availablestateitems in separate application scoped managed bean, list of available items initialized once during application's lifetime.

please note above answer jsf 1.2 targeted, since jsf 2.x not need convert selectitem[] anymore, state[] obtained state.values() has been sufficient.

see also:


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -