JDK7中菱形界面的意外行为 [英] Unexpected behaviour of diamond interface in JDK7

查看:162
本文介绍了JDK7中菱形界面的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSF托管bean中有一个属性:

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<>();

我得到的是 java.lang.ClassCastException:java.lang。当它跳到这个foreach上时,无法将字符串强制转换为java.lang.Long

for (Long CRC : selectedDataSets) { ... }

这很奇怪。调试显示 selectedDataSets 充满了 String 值,但我认为甚至不可能。请描述一下这里到底发生了什么。

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.

推荐答案

显然你将属性绑定到 UISelectMany 组件,如 < h: selectManyCheckbox> < selectManyListbox> ,未明确指定 Converter 。在Java中,泛型类型在运行时擦除和JSF(更具体地说,EL)对通用列表一无所知输入并默认为 字符串 除非另有说明 转换器 。这是 String 因为这只是 HttpServletRequest#getParameterMap() 。 EL通过反射填写提交值的列表,并且不接受通用类型考虑在内。

Apparently you bound the property to an UISelectMany component like <h:selectManyCheckbox> or <selectManyListbox> without explicitly specifying a Converter. 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 to String unless told otherwise by a Converter. It's String because that's just the default value type of HttpServletRequest#getParameterMap(). EL fills the list with submitted values by reflection and does not take any generic types into account.

所以,例如,这应该为你做,在内置的帮助 LongConverter

So, for example this should do it for you, with help of the builtin LongConverter:

<h:selectManyCheckbox value="#{bean.selectedDataSets}" converter="javax.faces.Long">



参见:




  • 在h:selectManyCheckbox中使用enum

  • See also:

    • Use enum in h:selectManyCheckbox
    • 请注意,这与Java 7的钻石操作符无关。当您尝试使用 new ArrayList< Long>()时,您会遇到完全相同的问题。

      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>().

      这篇关于JDK7中菱形界面的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆