I have a property in my JSF managed bean:
private List<Long> selectedDataSets;
I initialize the list like this within an other method:
ArrayList<Long> longList = new ArrayList<>();
What happens is I get java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long right when it jumps on this foreach:
for (Long CRC : selectedDataSets) { ... }
Which is very odd. Debug shows that selectedDataSets are full of String values, but I thought that’s not even possible. Please describe me what exactly happened here.
Apparently you bound the property to an
UISelectManycomponent like<h:selectManyCheckbox>or<selectManyListbox>without explicitly specifying aConverter. In Java, the generic type is erased during runtime and JSF (more specifically, EL) does not know anything about the generic list type at all and defaults toStringunless told otherwise by aConverter. It’sStringbecause that’s just the default value type ofHttpServletRequest#getParameterMap(). EL fills the list with submitted values by reflection and does not take any generic types into account.So, for example this should do it for you, with help of the builtin
LongConverter:See also:
Note that this has nothing to do with Java 7’s diamond operator. You would have exactly the same problem when you have experimented with
new ArrayList<Long>().