How to compare a char property in EL

This is, unfortunately, expected behavior. In EL, anything in quotes like 'N' is always treated as a String and a char property value is always treated as a number. The char is in EL represented by its Unicode codepoint, which is 78 for N.

There are two workarounds:

  1. Use String#charAt(), passing 0, to get the char out of a String in EL. Note that this works only if your environment supports EL 2.2. Otherwise you need to install JBoss EL.

    <h:commandButton ... rendered="#{product.orderStatus eq 'N'.charAt(0)}">
    
  2. Use the char’s numeric representation in Unicode, which is 78 for N. You can figure out the right Unicode codepoint by System.out.println((int) 'N').

    <h:commandButton ... rendered="#{product.orderStatus eq 78}">
    

The real solution, however, is to use an enum:

public enum OrderStatus {
     N, X, Y, Z;
}

with

private OrderStatus orderStatus; // +getter

then you can use exactly the desired syntax in EL:

<h:commandButton ... rendered="#{product.orderStatus eq 'N'}">

Additional bonus is that enums enforce type safety. You won’t be able to assign an aribtrary character like or as order status value.

Leave a Comment