如何显示< h:selectOneMenu>的选定值的标签在< h:outputText>中? [英] How to display the label of a selected value of a <h:selectOneMenu> in a <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>
我想显示选择的值,但是以下仅显示1
或2
.
I want to display the selected value, but the following only shows 1
or 2
.
<h:outputText value="#{bean.selectedCity}" />
我要显示标签NewYork
或Washington
.我该怎么办?
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.
这篇关于如何显示< h:selectOneMenu>的选定值的标签在< h:outputText>中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!