Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes JSF and the fly likes contains() always returning false Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "contains() always returning false" Watch "contains() always returning false" New topic

contains() always returning false

M Sham

Joined: Oct 18, 2012
Posts: 1
I'm trying to redo how a website handles permission access to elements. Right now we have it so when someone logs in (ActiveUser) they have an ArrayList of Integers corresponding to the FunctionIDs they are permitted access to. For example when I log in I have the values 1, 2, 6, 7 in my ActiveUser.permissions List.

The problem is that I'm trying to render elements in JSF using <h:tag rendered="#{activeUser.permissions.contains(x)}"> for elements. This is always returning false regardless of the values in the List. I have verified that the value is in fact in my List using Debugging mode.

Also, if I replace #{activeUser.permissions.contains(1)} with #{activeUser.permissions.get(0) == 1} it works and the element renders. I know that .contains(<literal int>) works for java.lang.Integers, so I have the feeling this has something to do with JSF.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16541

The first question is: do you actually need View-side logic here? Putting logic in VIews is something you should not do unless there's no simpler choice. Among the reasons for that is that you run into problems like the one you have.

If your function choices can be displayed in tabular form, for example, a cleaner way to do it is to house them in a dataTable and build a corresponding DataModel that only contains the elements for the allowed functions. That can all be done within the backing bean and you won't have to get into fights with EL or View-side logic.

Otherwise, I can't really tell what's wrong with ""#{activeUser.permissions.contains(x)}"" because I don't know what datatype "x" is supposed to be or where "x" is coming from.

Customer surveys are for companies who didn't pay proper attention to begin with.
I agree. Here's the link:
subject: contains() always returning false