1

I'm trying to conditionally render a primefaces fieldset using

<p:fieldset legend="content" rendered="#{controller.object.list.contains('THREAD_PROTRUSION')}" >

where 'THREAD_PROTRUSION' is an enum. The fieldset was not rendering when I felt it should so I added these outputs to my page:

#{controller.object.list}<br/>
#{controller.object.list.contains('THREAD_PROTRUSION')}<br/>

and the following is displayed

[THREAD_PROTRUSION, ENGAGE_NUT]
false

Why is the value false when 'THREAD_PROTRUSION' is in the list?

There is a little more to this. The list is set from checkboxes on another screen and are populated by loading an JSON input file. If I go back to the view with the checkboxes and cycle them (uncheck, then recheck) then go back to the view with my fieldset, I see

[THREAD_PROTRUSION, ENGAGE_NUT]
true

and of course now the fieldset is rendered

== MCV Example

view.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:o="http://omnifaces.org/ui" xmlns:p="http://primefaces.org/ui" xmlns:bjap="http://bjap.gdeb.com/facelets">

<h:body>
                    <li><b>Enum List </b> #{bean.myList}</li>
                    <li><b>bean.myList[0] </b>#{bean.myList[0]}</li>
                    <li><b>bean.myList[1] </b>#{bean.myList[1]}</li>
                    <li><b>bean.myList.contains('RED') </b>#{bean.myList.contains('RED')}</li>
                    <li><b>bean.myList[0] eq 'RED' </b>#{bean.myList[0] eq 'RED'}</li>
                    <li><b>bean.myList[1] eq 'RED' </b>#{bean.myList[1] eq 'RED'}</li>
</h:body>
</html>

Bean.java

@Named
public class Bean implements Serializable {
    private static final long serialVersionUID = 1L;

    public enum Color {
        WHITE, BLACK, RED, YELLOW, BLUE
    }

    private List<Color> myList;

    public Bean() {
        myList = new ArrayList<>();
        myList.add(Color.WHITE);
        myList.add(Color.RED);
    }

    public List<Color> getMyList() {
        return myList;
    }

    public void setMyList(List<Color> myList) {
        this.myList = myList;
    }

}

output

Enum List  [WHITE, RED]
bean.myList[0] WHITE
bean.myList[1] RED
bean.myList.contains('RED') false <-Expected to be true???
bean.myList[0] eq 'RED' false
bean.myList[1] eq 'RED' true
jeff
  • 3,618
  • 9
  • 48
  • 101
  • I improved your title and tagging a little. Can you make an [mcve]? And post version info (jsf impl, EL) sonce I cannot reproduce – Kukeltje Oct 25 '17 at 20:30
  • ok thanks. I'll work on a complete example – jeff Oct 25 '17 at 20:40
  • But make it **minimal** too, and verifyable – Kukeltje Oct 25 '17 at 20:46
  • Sorry, I had a typo in my own created example and therefor it 'worked'. I can reproduce now, but at the same time I found a 'duplicate' Q/A in stackoverflow. It has a very similar title and I wonder why it is not visible now in the 'related' Q/A part at the right... https://stackoverflow.com/questions/35558683/el-syntax-to-check-if-a-set-contains-a-specific-enum-value – Kukeltje Oct 26 '17 at 18:38
  • Possible duplicate of [EL syntax to check if a set contains a specific Enum value](https://stackoverflow.com/questions/35558683/el-syntax-to-check-if-a-set-contains-a-specific-enum-value) – Kukeltje Oct 26 '17 at 18:38
  • Maybe this is a better 'duplicate' since your real answer (since you are using facelets) is in the link referred to in the 'duplicate' : https://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el – Kukeltje Oct 26 '17 at 18:48
  • I guess we were both posting at the same time... – jeff Oct 26 '17 at 18:49

1 Answers1

0

Based on this answer EL syntax to check if a set contains a specific Enum value I now understand why this was false when I expected it to be true.

#{controller.object.list.contains('THREAD_PROTRUSION')}

As I mentioned in my question above, the list was initially populated by loading JSON(fasterxml Jackson). The list backs a set of Primefaces checkboxes.

I discovered that Jackson loaded the checkbox values as Enums (as would be expected) but when I manually selected the checkboxes the list was populated with Strings. I discovered this by iterating over my list and checking each value with instanceof against a String and my Enum.

When the list contains strings the EL list.contains worked so my work around was to change my list

from 
List<MyEnum> list;
to 
List<String> list;

With this change Jackson still seems to serialize and deserialize my checkbox values just fine and I can use EL to check of the list contains an Enum.

jeff
  • 3,618
  • 9
  • 48
  • 101