1

this is my first post in the forum. I have a Java enum class with identifier (value) and 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 the controller:

@Named(value = "auxCriteriExclusioController")
@ApplicationScoped
public class AuxCriteriExclusioController {   
    public CriteriExclusio[] getCriteriExclusio(){
        return CriteriExclusio.values();
    }
}

I load values-text in selectonemenu properly and stored in the table (values-int) when I submit the 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 is when a page retrieve the data in the table I want to display the text (text) in a outputlabel which corresponds to the value (value) that is stored in the database. It is much more user friendly and need to show the text not the numerical value.

I have searched and tried several options but I can not retrieve the text. Only recover the C1, C2....etc of java enum.

<p:outputLabel value="#{auxCriteriExclusioController.criteriExclusio[itemCriterisExclusio.idCriteriExclusio]}" />

For example if in the table is saved the value 3, the label to be displayed is "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> 
mabi
  • 5,279
  • 2
  • 43
  • 78
Xavi Filets
  • 21
  • 1
  • 5

4 Answers4

0

Why don't you save the complete enum value into mbVCriteriExclusio.criteriExclusio? JSF-2 automatically converts your enum value (see this question).

So when you 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 to display a label like this:

<h:outputLabel value="#{bean.criteriExclusio.text}" />
Community
  • 1
  • 1
mabi
  • 5,279
  • 2
  • 43
  • 78
0

I can not save in the bean because it is an iteration within a datatable and the values are saved in the datatable "var" parameter.

In the end I made a converter. I do not think that is an optimal solution but it works.

@FacesConverter("criteriExclusioConverter")
public class CriteriExclusioConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
      return null;

}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {

    String textCriteriExclusio;

    switch (value.toString()) {
        case "1":
            textCriteriExclusio = "< 10";
            break;
        case "2":
            textCriteriExclusio = "Low grade";
            break;
        case "3":
            textCriteriExclusio = "MEdium grade";
            break;
        case "4":
            textCriteriExclusio = "High grade";
            break;
        case "5":
            textCriteriExclusio = "> 250";
            break;
        default:
            textCriteriExclusio = "";
            break;
    }


    return textCriteriExclusio;


}

}

Xavi Filets
  • 21
  • 1
  • 5
  • Huh? That sounds like the worst solution, repeating your enum definition in the converter. Can you edit your answer with that datatable you mentioned? We might be able to help you find a better solution. – mabi Nov 13 '14 at 10:05
0

Another alternative to using expression language is using a ViewHelper Pattern.

I would suggest you to create a new HelperBean with a helper method to get the actual text.

@Named(value = "beanHelper")
@ApplicationScoped
public class BeanHelper {
   public String getCriteriText(int idCriteria){
      return auxCriteriExclusioController.getCriteriExclusio()[idCriteria].getText();
   }
}

And call this helper method from JSF:

<p:outputLabel value="#{beanHelper.getCriteriText(itemCriterisExclusio.idCriteriExclusio)}"></p:outputLabel>
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28
0

The helper does not work for me.

My managed bean helper MbADescripcionesHelper .java:

@Named(value = "mbADescripcionesHelper")
@ApplicationScoped
public class MbADescripcionesHelper implements Serializable {

@ManagedProperty(value = "#{auxCriteriExclusioController}")
private AuxCriteriExclusioController auxCriteriExclusioController;

public MbADescripcionesHelper() {
}

public String getCriteriText(int idCriteria) {
    return auxCriteriExclusioController.getCriteriExclusio()[idCriteria].getText();
}

public void setAuxCriteriExclusioController(AuxCriteriExclusioController auxCriteriExclusioController) {
    this.auxCriteriExclusioController = auxCriteriExclusioController;
}
}

xhtml line error:

<p:outputLabel value="#{mbADescripcionesHelper.getCriteriText(itemCriterisExclusio.idCriteriExclusio)}"></p:outputLabel>

error:

Error Rendering View[/revisions/PendentsRevisar.xhtml] javax.el.ELException: PendentsRevisar.xhtml @76,145 value="#{mbADescripcionesHelper.getCriteriText(itemCriterisExclusio.idCriteriExclusio)}": java.lang.NullPointerException

Xavi Filets
  • 21
  • 1
  • 5