0

I query from database and I receive an integer list. Ex: 0, 1, 2
If I display the digits to browser, users will not understand the meaning of number.
So, I would like to map a digit to a string.
Ex: 0: Pending, 1: Active, 2: Inactive, so on.
File display.xhtml will have the source code follow as:

<!--display.xhtml-->
<t:dataTable id="itemTable" value="#{itemBrowser.itemList}" var="item">
  <t:column>
    <f:facet name="header">
      <h:outputText value="Status" />
    </f:facet>
    <h:outputText value="#{itemStatusListReversedString[item.status]}" />
  </t:column>         
</t:dataTable>  

<!--faces-config.xml-->
<managed-bean>
  <managed-bean-name>itemStatusListReversedString</managed-bean-name>
  <managed-bean-class>java.util.HashMap</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <map-entries>
    <key-class>java.lang.String</key-class>
    <map-entry>
      <key>0</key>
      <value>Inactive</value>
    </map-entry>
    <map-entry>
      <key>1</key>
      <value>Active</value>
    </map-entry>
    <map-entry>
      <key>2</key>
      <value>Pending</value>
    </map-entry>
  </map-entries>
</managed-bean> 

But, there is nothing to output in browser. So, how can I fix this issue?

Thanks

Thinh Phan
  • 655
  • 1
  • 14
  • 27

2 Answers2

1

I think the problem is in this line:

<h:outputText value="#{itemStatusListReversedString[item.status]}" />

You have to do something like this

<h:outputText value="#{item.stringValue}" />

and in the item class add something like this:

public String getStringValue(){
 return itemStatusListReversedString.get(this.numberValue);
}

You must change the item class previously in the faces-config to inject a itemStatusListReversedString.

Example:

itemBrowser.itemList is a List of a objects of MyClass:

public class MyClass{
//The necessary stuff
private Integer valueFromDB; //0, 1, 2...
private Map<Integer, String> itemStatusListReversedString; //The map you configured in the faces-config.xml

//More stuff

public String getStringValue(){
 return itemStatusListReversedString.get(this.valueFromDB);
}

}

In the faces-config.xml you configure MyClass this way:

<bean id="myClassInstance"              
class="package.MyClass" scope="request">    
        <property name="itemStatusListReversedString" ref="itemStatusListReversedString"></property>
    </bean>

When creating new instances of MyClass use this approach instead of creating them using new MyClass():

WebApplicationContext webApplicationContext = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        MyClass bean = (MyClass)webApplicationContext.getBean("myClassInstance");
Averroes
  • 4,168
  • 6
  • 50
  • 63
0

Use enum then work with ordinal() for the numbers and values() for the text like:

YourEnum.values()[ordinal]
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130