问题描述
我有一个包含以下条目的资源包:
I have a resource bundle with entries like these:
entry1=value1
entry2=value2
entry3=value3
在我的 JSF 页面中,我尝试动态使用这些键.条目的 ID 来自托管 bean.我认为应该是这样的:
In my JSF page I'm trying to use these keys dynamically. The ID of the entry is coming from a managed bean. I think it should be something like this:
<h:outputText value="#{msg['entry' managedBean.entryIndex]}"/>
我怎样才能做到这一点?
How can I achieve this?
推荐答案
如果您已经在使用 Servlet 3.1/EL 3.0(Tomcat 8、WildFly 8、GlassFish 4 等),请使用新的 EL 3.0 +=
运算符:
If you're already on Servlet 3.1 / EL 3.0 (Tomcat 8, WildFly 8, GlassFish 4, etc), make use of new EL 3.0 +=
operator:
<h:outputText value="#{msg['entry' += managedBean.entryIndex]}" />
如果您只使用 Servlet 3.0/EL 2.2(Tomcat 7、JBoss AS 6/7、GlassFish 3 等),请利用新的 EL 2.2 功能直接调用诸如 String#concat()
:
If you're only on Servlet 3.0 / EL 2.2 (Tomcat 7, JBoss AS 6/7, GlassFish 3, etc), make use of new EL 2.2 ability to directly invoke methods such as String#concat()
:
<h:outputText value="#{msg['entry'.concat(managedBean.entryIndex)]}" />
如果您甚至还没有使用 Servlet 3.0/EL 2.2,请使用 <c:set>
来创建另一个内联所需 EL 表达式的变量:
If you're even not on Servlet 3.0 / EL 2.2 yet, make use of <c:set>
to create another variable with the desired EL expression inlined:
<c:set var="key" value="entry#{managedBean.entryIndex}" />
<h:outputText value="#{msg[key]}" />
这篇关于EL 中用于动态 ResourceBundle 键的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!