jaykio77
jaykio77

Reputation: 391

Using Ternary Operator to implement else if in EL - jsf

I have an application being developed in jsf 2.0, primefaces and using Eclipse Kepler IDE. I need to display a string value in dataTable for long value. There could be 6 possible values from 1 to 6. i have followed this question to solve my issue but i cant. my code snipped is

  <p:dataTable var="student" value="#{studentBean.studentList}">
  <p:column headerText="Class">
    <h:outputText value="#{student.studentClass == 1? 'One' :
                           student.studentClass == 2? 'Second' :
                           student.studentClass == 3? 'Third' :
                           student.studentClass == 4? 'Fourth' :
                           student.studentClass == 5? 'Fifth':
                           student.studentClass == 6? 'Sixth':''}" />
  </p:column>
      ....

i also tried:-

   student.studentClass.equals(1l) and  student.studentClass.equals(1L)

but no luck. What am i doing wrong

Upvotes: 2

Views: 2105

Answers (2)

Rene Enriquez
Rene Enriquez

Reputation: 1609

I would prefer adding a simple change in the model, say:

Class Pojo/Entity

public class Student{

   ...

   // Add transient annotation only if is an entity class
   @Transient
   private String valueToShow;

   public String getValueToShow(){
     if("1".equals(this.studentClass){
       return "One";
     } else if("2".equals(this.studentClass){
       return "Two";
     }
       ...
  }

}

Then, add this change to the xhtml file (JSF Page):

<p:dataTable var="student" value="#{studentBean.studentList}">
  <p:column headerText="Class">
    <h:outputText value="#{student.valueToShow}" />
  </p:column>
  ....

Upvotes: 1

raven
raven

Reputation: 2431

Wouldn't this approach work too?

<h:outputText rendered="#{student.studentClass == 1}" value="One" />
<h:outputText rendered="#{student.studentClass == 2}" value="Two" />
 ...
<h:outputText rendered="#{student.studentClass == 6}" value="Six" />

Upvotes: 1

Related Questions