21

I have a resource bundle with entries like these:

entry1=value1
entry2=value2
entry3=value3

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
gulygab
  • 245
  • 1
  • 3
  • 7

4 Answers4

47

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]}" />

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)]}" />

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]}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i hoped there is a one-line way for this, but this facelet-version is good enough. thanks a lot – gulygab Jan 31 '11 at 15:32
  • You're welcome. Note that this is an EL restriction, not a JSF restriction. EL has no facilities for string concatenation inside an expression. – BalusC Jan 31 '11 at 15:32
8

This should solve your issue:

<h:outputText value="#{msg['entry'.concat(managedBean.entryIndex)]}"/>
kojote
  • 81
  • 1
  • 1
2

Here's what worked for me : concat outside of []

<h:outputText value="#{msg['entry'].concat(managedBean.entryIndex)}" />
hmzn
  • 321
  • 2
  • 6
  • 22
0

I think you'll have to write a facelets function concat(str1, str2).

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140