JSF 2.0 - How to show in a label the text value of java enum? -


this first post in forum. have java enum class identifier (value) , description (text).

criteriexlusio.java:

public enum criteriexclusio{ c1(1, "< 10"), c2(2, "low grade"), c3(3, "medium grade"), c4(4, "high grade"), c5(5, "> 250"),  private final int value; private final string text;  private criteriexclusio(int value, string text) {     this.value = value;     this.text = text; }  public int getvalue() { return value; } public string gettext() { return text; }     } 

and controller:

@named(value = "auxcriteriexclusiocontroller") @applicationscoped public class auxcriteriexclusiocontroller {        public criteriexclusio[] getcriteriexclusio(){         return criteriexclusio.values();     } } 

i load values-text in selectonemenu , stored in table (values-int) when submit form.

.xhtml

<h:selectonemenu value="#{mbvcriteriexclusio.criteriexclusio.idcriteriexclusio}" id="cmbcriteriexclusio" required="true">   <f:selectitem itemlabel="" itemvalue=""></f:selectitem>   <f:selectitems value="#{auxcriteriexclusiocontroller.criteriexclusio}" var="respuestacriterisexclusio" itemvalue="#{respuestacriterisexclusio.value}" itemlabel="#{respuestacriterisexclusio.text}" /> </h:selectonemenu>  

the problem when page retrieve data in table want display text (text) in outputlabel corresponds value (value) stored in database. more user friendly , need show text not numerical value.

i have searched , tried several options can not retrieve text. recover c1, c2....etc of java enum.

<p:outputlabel value="#{auxcriteriexclusiocontroller.criteriexclusio[itemcriterisexclusio.idcriteriexclusio]}" /> 

for example if in table saved value 3, label displayed "medium grade"

update. datatable code

<h:form id="frmexclos">                         <p:growl id="mensajegeneral3" sticky="false" showdetail="true"/>                         <p:panel id="pnlcriteriexclusio" style="width: 425px" header="criteris d'exclusió del pacient" widgetvar="pnlcriterise">                             <p:datatable id="tblcriterisexclusionia" var="itemcriterisexclusio" value="#{mbrcriteriexclusio.getcriterisexclusionia(mbvmalignitatnia.personaambmalignitatnia.id)}" editable="true">                                 <p:ajax event="rowedit" listener="#{mbrcriteriexclusio.onrowedit}" update=":frmexclos:mensajegeneral3" />                                 <p:ajax event="roweditcancel" listener="#{mbrcriteriexclusio.onrowcancel}" update=":frmexclos:mensajegeneral3"  />                                         <p:column headertext="criteri">                                         <p:celleditor>                                             <f:facet name="output">                                                 <p:outputlabel value="#{itemcriterisexclusio.idcriteriexclusio}">                                                   <f:converter converterid="criteriexclusioconverter"/>                                                 </p:outputlabel>                                             </f:facet>                                             <f:facet name="input">                                                 <h:selectonemenu value="#{itemcriterisexclusio.idcriteriexclusio}" id="cmbcriteriexclusioeditat" required="true">                                                     <f:selectitem itemlabel="" itemvalue=""></f:selectitem>                                                     <f:selectitems value="#{auxcriteriexclusiocontroller.criteriexclusio}" var="respuestacriterisexclusioeditar" itemvalue="#{respuestacriterisexclusioeditar.value}" itemlabel="#{respuestacriterisexclusioeditar.text}" />                                                 </h:selectonemenu>                                              </f:facet>                                         </p:celleditor>                                     </p:column>                                     <p:column headertext="observacions">                                         <p:celleditor>                                             <f:facet name="output"><h:outputtext value="#{itemcriterisexclusio.comentaris}"></h:outputtext></f:facet>                                             <f:facet name="input"><p:inputtext value="#{itemcriterisexclusio.comentaris}" label="observacions"></p:inputtext></f:facet>                                         </p:celleditor>                                     </p:column>                                     <p:column style="width:32px">                                         <p:roweditor />                                     </p:column>                                     <f:facet name="footer" >                                         <p:commandbutton update="@this" icon="ui-icon-plusthick" value="afegir criteri" oncomplete="pf('dlgaddcriterisexclusio').show()"/>                                     </f:facet>                             </p:datatable>                         </p:panel>                     </h:form>  

why don't save complete enum value mbvcriteriexclusio.criteriexclusio? jsf-2 automatically converts enum value (see this question).

so when use this:

<h:selectonemenu value="#{bean.criteriexclusio}" required="true">   <f:selectitem itemlabel="" itemvalue="" noselectoption="true"></f:selectitem>   <f:selectitems value="#{valueprovider.criteriexclusio}"        var="crit" itemlabel="#{crit.text}" /> </h:selectonemenu> 

you can use bean.criteriexclusio of type criteriexclusio display label this:

<h:outputlabel value="#{bean.criteriexclusio.text}" /> 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -