Dominik,

The best way to do this will be by using a custom Converter. This way, your custom Converter class can contain the logic to 'translate' between e.g. the categoryId and the Category description. Your managed bean can remain unchanged and unaware of presentation layer issues.

A good example of how to build such a custom Converter can be found at [1] under the heading Custom Converters. The example shown there uses <h:inputText/>, but exactly the same thing also works for <h:outputText/> as you're trying to do. Just implement your Converter's getAsString() to return the correct full description for the categoryId and you should be fine...

Regards,

Gert Vanthienen
[EMAIL PROTECTED]

[1] http://www-128.ibm.com/developerworks/java/library/j-jsf3/

Bieringer Dominik 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

------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.428 / Virus Database: 268.13.11/496 - Release Date: 24/10/2006 
22:10

Reply via email to