如何显示< h:selectOneMenu>的选定值的标签在< h:outputText>中? [英] How to display the label of a selected value of a <h:selectOneMenu> in a <h:outputText>?

查看:87
本文介绍了如何显示< h:selectOneMenu>的选定值的标签在< h:outputText>中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在<h:outputText />中显示所选值<h:selectOneMenu />的标签.

I want to show the label of the selected value of <h:selectOneMenu /> in a <h:outputText />.

我在下面的下拉列表中获得了选定的值.

I have the following dropdown where I get the selected value.

<h:column>
    <h:selectOneMenu value="#{bean.selectedCity}">
        <f:selectItem itemValue="1" itemLabel="NewYork"/>
        <f:selectItem itemValue="2" itemLabel="Washington"/>
    </h:selectOneMenu>
</h:column>

我想显示选择的值,但是以下仅显示12.

I want to display the selected value, but the following only shows 1 or 2.

<h:outputText value="#{bean.selectedCity}" />

我要显示标签NewYorkWashington.我该怎么办?

I want to display the label NewYork or Washington. How can I do this?

推荐答案

更新#2 基于新的修改和评论:啊,我们最终到达了某个地方(我删除了完整的旧答案,如果您仍然想查看,请检查编辑历史记录.

Update #2 based on the new edits and comments: ah we finally get somewhere (I removed the entire old answer, check edit history if you want to see it anyway).

您只需要在模型中的某处维护一个Map<Long, String> cities之类的东西,然后按以下方式使用它即可:

You just need to maintain something like a Map<Long, String> cities somewhere in your model and then use it as follows:

<h:outputText value="#{bean.cities[bean.selectedCity]}" />

这将基本上显示bean.getCities().get(bean.getSelectedCity());.您甚至可以将地图重新​​用于<f:selectItems>,这样就无需在两个位置进行维护.

This will basically display bean.getCities().get(bean.getSelectedCity());. You could even reuse the map for <f:selectItems> so that you don't need to maintain it in two places.

这篇关于如何显示&lt; h:selectOneMenu&gt;的选定值的标签在&lt; h:outputText&gt;中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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