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

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

问题描述

当我使用 f:selectItems 在 Map 中显示项目时,我无法显示 Map 项目的值,只能显示键.f:selectItems 根本不使用 itemLabel.当我使用 List 时,事情会起作用.

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>

以下尝试在 Map 中显示项目的值不起作用.它显示项目的键,但不使用 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 的 Map 有什么想法吗?

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.

对于Map的具体问题,你需要意识到map键默认情况下用作项目标签,并且地图值默认用作项目值.您似乎希望它是相反的(老实说,我直觉上也希望如此,但这只是一个设计决定——地图键强制唯一性和选项标签在 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 页面
  • 这篇关于JSF-2 f:selectItems with Map 不显示 itemLabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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