Below is the jsf code
下面是jsf代码
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject}"var="entity"
itemValue="#{entity.key}" itemLabel="#{entity.value.code}"/>
</h:selectOneMenu>
Backing bean has map defined as
Backing bean将map定义为
private Map<TestClass, Object> mapObject;
TestClass has two variables and getters/setters
TestClass有两个变量和getter / setter
String code;
String name;
Issue I am having is : On JSF, on Selectonemenu, for itemLabel i would like to display the TestClass String value i.e code should be displayed.
我遇到的问题是:在JSF上,在Selectonemenu上,对于itemLabel,我想显示TestClass字符串值,即应显示代码。
Presently on SelectOneMenu on UI, I am getting
目前在UI上的SelectOneMenu上,我得到了
TestClass[code=t,name=anu] on the drop down, instead i want code value i..e 't'
Please let me know how to acheive this.
请让我知道如何实现这一目标。
2 个解决方案
#1
The <f:selectItems var>
should in case of maps specify the Map.Entry
. The <f:selectItems value>
should therefore specify Collection<Map.Entry>
. This is not natively recognized yet when you just specify a Map
(will come in JSF 2.3).
You basically need to explicitly set the value to Map#entrySet()
as below in case you intend to use map key as item value and ma:
您基本上需要将值显式设置为Map#entrySet(),如果您打算使用map键作为项值和ma:
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}"var="entry"
itemValue="#{...}" itemLabel="#{...}" />
</h:selectOneMenu>
The Map.Entry
object has getKey()
and getValue()
methods which return respectively the map entry's key and map entry's value, which are in your case respectively TestClass
and Object
. You seem to want to display the code
property of TestClass
as item label. So, this should do:
Map.Entry对象具有getKey()和getValue()方法,它们分别返回映射条目的键和映射条目的值,在您的情况下分别为TestClass和Object。您似乎想要将TestClass的代码属性显示为项标签。所以,这应该做:
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}"var="entry"
itemValue="#{...}" itemLabel="#{entry.key.code}" />
</h:selectOneMenu>
You're not terribly clear on what exactly you'd like to use as item value, so I've left it open.
你不清楚你想用什么作为物品价值,所以我把它打开了。
See also:
- Our
selectOneMenu
wiki page
我们的selectOneMenu维基页面
#2
You need to change the itemlabel
attribute, (with current value entity.value.code
). It should be replaced by entity.key.code
?
您需要更改itemlabel属性(使用当前值entity.value.code)。它应该被entity.key.code取代?
Also the value
needs to call the method .entrySet
, because the map in itself is not a collection.
此值还需要调用方法.entrySet,因为映射本身不是集合。
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.key.code}"/>
</h:selectOneMenu>
Alternatively, since it's the key you want, you could iterate the keys immediately.
或者,因为它是您想要的密钥,您可以立即迭代密钥。
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.keySet()}" var="key"
itemValue="#{key}" itemLabel="#{key.code}"/>
</h:selectOneMenu>
#1
The <f:selectItems var>
should in case of maps specify the Map.Entry
. The <f:selectItems value>
should therefore specify Collection<Map.Entry>
. This is not natively recognized yet when you just specify a Map
(will come in JSF 2.3).
You basically need to explicitly set the value to Map#entrySet()
as below in case you intend to use map key as item value and ma:
您基本上需要将值显式设置为Map#entrySet(),如果您打算使用map键作为项值和ma:
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}"var="entry"
itemValue="#{...}" itemLabel="#{...}" />
</h:selectOneMenu>
The Map.Entry
object has getKey()
and getValue()
methods which return respectively the map entry's key and map entry's value, which are in your case respectively TestClass
and Object
. You seem to want to display the code
property of TestClass
as item label. So, this should do:
Map.Entry对象具有getKey()和getValue()方法,它们分别返回映射条目的键和映射条目的值,在您的情况下分别为TestClass和Object。您似乎想要将TestClass的代码属性显示为项标签。所以,这应该做:
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}"var="entry"
itemValue="#{...}" itemLabel="#{entry.key.code}" />
</h:selectOneMenu>
You're not terribly clear on what exactly you'd like to use as item value, so I've left it open.
你不清楚你想用什么作为物品价值,所以我把它打开了。
See also:
- Our
selectOneMenu
wiki page
我们的selectOneMenu维基页面
#2
You need to change the itemlabel
attribute, (with current value entity.value.code
). It should be replaced by entity.key.code
?
您需要更改itemlabel属性(使用当前值entity.value.code)。它应该被entity.key.code取代?
Also the value
needs to call the method .entrySet
, because the map in itself is not a collection.
此值还需要调用方法.entrySet,因为映射本身不是集合。
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.entrySet()}" var="entry"
itemValue="#{entry.key}" itemLabel="#{entry.key.code}"/>
</h:selectOneMenu>
Alternatively, since it's the key you want, you could iterate the keys immediately.
或者,因为它是您想要的密钥,您可以立即迭代密钥。
<h:selectOneMenu>
<f:selectItems value="#{bean.mapObject.keySet()}" var="key"
itemValue="#{key}" itemLabel="#{key.code}"/>
</h:selectOneMenu>