I "cheated" and made an interface for enum types:
public interfaces EnumDescribed
{
public String getBundleKey();
}
public enum TestEnum
implements EnumDescribed
{
A("test.a"),
B("test.b");
private final String bundleKey;
private TestEnum(String bundleKey)
{
this.bundleKey = bundleKey;
}
public String getBundleKey()
{
return this.bundleKey;
}
}
public class ExampleBean
{
private TestEnum testEnum;
public TestEnum getTestEnum() { return this.testEnum; }
}
<t:outputText value="#{msg[exampleBean.testEnum.bundleKey]}" />
-Andrew
On 10/25/06, Bieringer Dominik <[EMAIL PROTECTED]> wrote:
Hi all,
today I've wondered again about how to convert property values of Beans to
locale specific strings in the presentation layer. To make it clear what I
want to do, I will show you a small example (This code is not compileable…
It's just for demonstration purpose):
Consider a class MyBean:
public class MyBean {
public String getCategoryId(); // Can get values 'CatA',
'CatB', 'CatC'
}
Now consider having JSF code like this:
<h:panelGroup>
<h:outputText value="#{myBean.categoryId}"/>
</h:panelGroup>
This would produce output like this on my webpage:
'CatA' or 'CatB' or 'CatC'
The problem is that I don't want to display these raw values of the
JavaBean, instead I want to display specific strings stored somewhere in the
presentation layer, for example in a message.properties file, which I can
use in the JSP file. But how to do that efficiently and what"s the best
practice for doing so? I frequently have this problem, not only with String
types, but also with Java 1.5 Enum types….
At the moment I am solving the problem the following way: I am using a
message.property file for JSF… which looks like the following:
Category_A=Category A
Category_B=Category B
Category_C=Category C
Next I am rewriting the JavaBean class, so that it look's like the
following:
public class MyBean {
public String getCategoryId(); // Can get values 'CatA',
'CatB', 'CatC'
public boolean getIsCategoryA() {
return (this.getCategoryId().equals("CatA"));
}
…
}
Then I can change my JSF code to look like the following:
<h:panelGroup>
<h:outputText value="#{Msg.Category_A}"
rendered="#{myBean.isCategoryA}"/>
<h:outputText value="#{Msg.Category_B}"
rendered="#{myBean.isCategoryB}"/>
<h:outputText value="#{Msg.Category_C}"
rendered="#{myBean.isCategoryC}"/>
</h:panelGroup>
I accepted this method at first, but as I've already said, I'm having this
problem very often and I don't feel comfortable about doing it that way..
that's not really beautiful and get's really cumbersome if there are more
than 3 different possibilities….
I know that there must be a mapping of the Java bean values and the
message.properties file, but I don't think that it's a good idea to change
the JavaBean class to reflect my need… For me the bean is not part of the
presentation layer any more….
I am looking forward to get some feedback and to hear about how you are
solving this problem.
Thx in advance,
Dominik