JSF-2 f:带有Map的selectItems不显示itemLabel [英] JSF-2 f:selectItems with Map does not display itemLabel

查看:433
本文介绍了JSF-2 f:带有Map的selectItems不显示itemLabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用f:selectItems显示地图中的项目时,我无法显示Map项目的值,只能显示该键。 f:selectItems根本不使用itemLabel。当我使用列表而不是工作。

When I use f:selectItems to display items in a Map I cannot display the value of the Map item, only the key. f:selectItems does not use the itemLabel at all. When I use a List instead things work.

以下内容使用itemLabel显示列表中项目的描述:

The following does make use of the itemLabel to display the "description" of an item in a List:

<h:selectOneMenu>
  <f:selectItems value="#{testBB.testList}" var="s"
    itemLabel="TEST #{s.description}" itemValue="#{TEST s.name}" />
</h:
selectOneMenu>

以下尝试在地图中显示项目的值不起作用。它显示项目的键,但不使用itemLabel属性,可以看出缺少TEST文本的输出。

The following attempt to display the value of an item in a Map does not work. It displays the item's key, but not using the itemLabel attribute, as can be discerned by that lack of output of the "TEST" text.

<rich:select>
  <f:selectItems value="#{testBB.testMap}" var="s"
    itemLabel="TEST #{s.value}" itemValue="TEST #{s.key}" />
</rich:select>

使用的简单支持bean如下:

The simple backing bean used follows:

public class TestBB {
  private Map<String, String> testMap;
  private List<TestItem> testList;

  public TestBB() {
    testMap = new HashMap<String, String>();
    testMap.put("1_key", "Item One");
    testMap.put("2_key", "Item Two");
    testMap.put("3_key", "Item Three");

    testList = new ArrayList<TestItem>();
    testList.add( new TestItem("name_1", "description_1") );
    testList.add( new TestItem("name_2", "description_2") );
    testList.add( new TestItem("name_3", "description_3") );
  }

  public Map<String, String> getTestMap() {
    return testMap;
  }

  public List<TestItem> getTestList() {
    return testList;
  }

}

所以,关于如何做这个工作,那就是如何有效地使用一个带有selectItems的地图?

So, any ideas on how to make this work, that is, how to effectively use a Map with selectItems?

推荐答案

你的问题是声音,它混淆和模糊。

Your question is sound, but the code makes it confusing and ambiguous. I'll just ignore your code in this answer.

关于具体问题如何使用地图< f:selectItems> 中,您需要意识到,默认情况下,地图键被用作项目标签,并且映射值是默认的被用作项目值。你似乎期望它是另一回事(老实说,我直观地也期待,但这仅仅是一个设计设计 - 地图关键点强制唯一性和选项标签应该在UI视角中绝对是唯一的,但是选项值不一定是唯一的)。

As to the concrete question "How to use Map in <f:selectItems>", you need to realize that map keys are by default been used as item labels and that map values are by default been used as item values. You seem to expect it to be the other way round (honestly, I'd intuitively also expect that, but that was just a design desicion --the map key forces uniqueness and option labels should in UI perspective definitely be unique, but option values does not necessarily need to be unique).

所以,这应该做(注意我使用 LinkedHashMap ,因为它维护插入顺序):

So, this should do (note that I use LinkedHashMap here as it maintains the insertion order):

map = new LinkedHashMap<String, String>();
map.put("Label 1", "value1");
map.put("Label 2", "value2");
map.put("Label 3", "value3");

<f:selectItems value="#{bean.map}" />

如果你想要交换键和值,那么你应该迭代 Map#entrySet() 。只有当您的环境支持EL 2.2时,您才能通过直接方法调用来调用它,因为它没有getter。

If you want so swap the keys and values, then you should be iterating over Map#entrySet(). This works only when your environment supports EL 2.2 as you have to invoke it by a direct method invocation as there's no getter for that.

例如

map = new LinkedHashMap<String, String>();
map.put("value1", "Label 1");
map.put("value2", "Label 2");
map.put("value3", "Label 3");

<f:selectItems value="#{bean.map.entrySet()}" var="entry" 
    itemValue="#{entry.key}" itemLabel="#{entry.value}" />



另请参见:




  • 我们的 selectOneMenu wiki页面

  • See also:

    • Our selectOneMenu wiki page
    • 这篇关于JSF-2 f:带有Map的selectItems不显示itemLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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