EL中用于动态ResourceBundle键的字符串连接 [英] String concatenation in EL for dynamic ResourceBundle key
问题描述
我有一个包含以下条目的资源包:
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]}"/>
我该如何实现?
推荐答案
如果您已经在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键的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!