You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Bieringer Dominik <Bi...@gmx.at> on 2006/10/25 22:02:40 UTC

Best Practice - Converting raw property values of Beans

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


Re: Best Practice - Converting raw property values of Beans

Posted by Torsten Krah <tk...@fachschaft.imn.htwk-leipzig.de>.
But what about f:selectItems?

Using a SelectOneMenu, the converter changes the "option" value - but
what i want is to change the value chown to the user, that one between
the begin and end of the option Tag.

<t:selectOneMenu forceId="true" id="languages"
value="#{@managed-bean-name.selectedLanguage}">
	<f:selectItems value="#{@managed-bean-name.allLanguages}" />
</t:selectOneMenu>

output:

<select>...<option value="THIS CHANGES">THIS I WANT TO
CHANGE</option>...</select>

The value is an id which should be the same - but the id + the view
locale defines the value which should be show there, e.g.: "german" or
"Deutsch".

How to do this?

Torsten

Am Mittwoch, den 25.10.2006, 22:29 +0200 schrieb Gert Vanthienen:
> 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
> gert@anova.be
> 
> [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
> >   
> 

AW: Best Practice - Converting raw property values of Beans

Posted by Bieringer Dominik <Bi...@gmx.at>.
Thx. Very much... I will read the docs tomorrow I think. It sounds really
promising ;)

-----Ursprüngliche Nachricht-----
Von: Gert Vanthienen [mailto:gert.vanthienen@skynet.be] 
Gesendet: Mittwoch, 25. Oktober 2006 22:29
An: MyFaces Discussion
Betreff: Re: Best Practice - Converting raw property values of Beans

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
gert@anova.be

[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
>   


Re: Best Practice - Converting raw property values of Beans

Posted by Gert Vanthienen <ge...@skynet.be>.
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
gert@anova.be

[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
>   


Re: Best Practice - Converting raw property values of Beans

Posted by Gilles Demarty <gi...@gmail.com>.
read the section 'Multilanguage Support' from
http://wiki.apache.org/myfaces/Create_and_Display_Messages

It may do the trick.


2006/10/25, Bieringer Dominik <Bi...@gmx.at>:
>
>
>
>
> 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

AW: AW: Best Practice - Converting raw property values of Beans

Posted by Bieringer Dominik <Bi...@gmx.at>.
Yes... that's true ;). I think i will try to use Covnerters, as Gert
Vanthienen suggested to me... I've already read some articles about
converters and it sounds like a convenient method... And I don't have to mix
presentation layer with business layer, which of course isn't possible
always anyway...

But thx for your answer, you helped me anyway (didn't knew it's possible to
access messages from the bundle in the way you've described.

Thx,
Dominik

-----Ursprüngliche Nachricht-----
Von: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com] 
Gesendet: Donnerstag, 26. Oktober 2006 17:22
An: MyFaces Discussion
Betreff: Re: AW: Best Practice - Converting raw property values of Beans

It is "cheating" because I put UI specific code in my enumeration (the
bundle key). I didn't feel that great about it, but it worked well
enough for me to use it.

On 10/25/06, Jonathan Harley <jo...@parkplatz.net> wrote:
> Bieringer Dominik wrote:
> > Hey cool... thx very much... I didn't knew about the possibility to
access
> > the message from the bundle via
> >
> > <t:outputText value="#{msg[exampleBean.testEnum.bundleKey]}" />
> >
> > That makes life easier.
>
> It does, but you still have to declare the message bundle for "msg".
> There's a technique I like that simplifies things even further, which
> is to use an EL function to do the bundle lookup - then you don't even
> have to declare the resource bundle, just the function namespace, which
> is the same on every page. That makes it easy to reorganise and rename
> message bundles without having to change every page.
>
> > -----Ursprüngliche Nachricht-----
> > Von: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com]
> > Gesendet: Mittwoch, 25. Oktober 2006 22:30
> > An: MyFaces Discussion
> > Betreff: Re: Best Practice - Converting raw property values of Beans
> >
> > I "cheated" and made an interface for enum types:
>
> (Why is this cheating?)
>
> > 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
>
> I've been using a similar technique, but it seems to me that there's
> no reason to invent those "test.a" strings and store them in your Enum.
> Why should the Enum have to know about the structure of resource
> bundles, which may be authored by different folks to the Java
> developers? So I use the Enum's name (A and B in the example above)
> with a method like this:
>
> public String getBundleKey() {
>    return name();
> }
>
> Or you could use the class name + enum name if you're worried about
> name clashes. Then the Enum doesn't need to know anything about the
> view layer.
>
>
> Jonathan
> --
> .....................................................................
>            Dr Jonathan Harley   .
>                                 .   Email: jon@parkplatz.net
>             Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
>             www.parkplatz.net   .   Mobile: 079 4116 0423
>


Re: AW: Best Practice - Converting raw property values of Beans

Posted by Andrew Robinson <an...@gmail.com>.
It is "cheating" because I put UI specific code in my enumeration (the
bundle key). I didn't feel that great about it, but it worked well
enough for me to use it.

On 10/25/06, Jonathan Harley <jo...@parkplatz.net> wrote:
> Bieringer Dominik wrote:
> > Hey cool... thx very much... I didn't knew about the possibility to access
> > the message from the bundle via
> >
> > <t:outputText value="#{msg[exampleBean.testEnum.bundleKey]}" />
> >
> > That makes life easier.
>
> It does, but you still have to declare the message bundle for "msg".
> There's a technique I like that simplifies things even further, which
> is to use an EL function to do the bundle lookup - then you don't even
> have to declare the resource bundle, just the function namespace, which
> is the same on every page. That makes it easy to reorganise and rename
> message bundles without having to change every page.
>
> > -----Ursprüngliche Nachricht-----
> > Von: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com]
> > Gesendet: Mittwoch, 25. Oktober 2006 22:30
> > An: MyFaces Discussion
> > Betreff: Re: Best Practice - Converting raw property values of Beans
> >
> > I "cheated" and made an interface for enum types:
>
> (Why is this cheating?)
>
> > 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
>
> I've been using a similar technique, but it seems to me that there's
> no reason to invent those "test.a" strings and store them in your Enum.
> Why should the Enum have to know about the structure of resource
> bundles, which may be authored by different folks to the Java
> developers? So I use the Enum's name (A and B in the example above)
> with a method like this:
>
> public String getBundleKey() {
>    return name();
> }
>
> Or you could use the class name + enum name if you're worried about
> name clashes. Then the Enum doesn't need to know anything about the
> view layer.
>
>
> Jonathan
> --
> .....................................................................
>            Dr Jonathan Harley   .
>                                 .   Email: jon@parkplatz.net
>             Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
>             www.parkplatz.net   .   Mobile: 079 4116 0423
>

Re: AW: AW: Best Practice - Converting raw property values of Beans

Posted by Jonathan Harley <jo...@parkplatz.net>.
Bieringer Dominik wrote:
> Okay, Where can i find information about the technique you are talking
> about?

As luck would have it, I wrote a blog entry about that technique (EL
functions) last week, but hadn't got round to posting it up yet. Your
question has motivated me to post it, so it's here:

http://jroller.com/page/ThoughtPark?entry=using_facelets_el_functions_for

The specifics of what I wrote are for EL functions in facelets, but the
same principle applies for JSPs, it's just slightly harder work.

I wrote it because you very rarely see EL functions mentioned. I
think they came in with JSP 2.0.

> I can do the same you said on my JSF pages too ;)... I am having one JSF
> page called main.jsp, which is including all other JSF pages dynamically...
> (The decision which page to include is stored in an Session Bean)
> 
> That way, the user always navigates to main.jsp, but different pages are
> loaded, .... Because of that, I only have to declare the message bundle
> once, in the main.jsp page, and the message bundle is available in all
> included sub pages.

If your message bundle is only declared in one place, there's not
much to be gained by my EL technique.

It sounds a bit like you're re-inventing the kind of templating
that facelets does. Have you thought about using it?


J.
-- 
.....................................................................
           Dr Jonathan Harley   .
                                .   Email: jon@parkplatz.net
            Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
            www.parkplatz.net   .   Mobile: 079 4116 0423

AW: AW: Best Practice - Converting raw property values of Beans

Posted by Bieringer Dominik <Bi...@gmx.at>.
Okay, Where can i find information about the technique you are talking
about?

I can do the same you said on my JSF pages too ;)... I am having one JSF
page called main.jsp, which is including all other JSF pages dynamically...
(The decision which page to include is stored in an Session Bean)

That way, the user always navigates to main.jsp, but different pages are
loaded, .... Because of that, I only have to declare the message bundle
once, in the main.jsp page, and the message bundle is available in all
included sub pages.

-----Ursprüngliche Nachricht-----
Von: Jonathan Harley [mailto:jon@parkplatz.net] 
Gesendet: Donnerstag, 26. Oktober 2006 00:23
An: MyFaces Discussion
Betreff: Re: AW: Best Practice - Converting raw property values of Beans

Bieringer Dominik wrote:
> Hey cool... thx very much... I didn't knew about the possibility to access
> the message from the bundle via 
> 
> <t:outputText value="#{msg[exampleBean.testEnum.bundleKey]}" />
> 
> That makes life easier.

It does, but you still have to declare the message bundle for "msg".
There's a technique I like that simplifies things even further, which
is to use an EL function to do the bundle lookup - then you don't even
have to declare the resource bundle, just the function namespace, which
is the same on every page. That makes it easy to reorganise and rename
message bundles without having to change every page.

> -----Ursprüngliche Nachricht-----
> Von: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com] 
> Gesendet: Mittwoch, 25. Oktober 2006 22:30
> An: MyFaces Discussion
> Betreff: Re: Best Practice - Converting raw property values of Beans
> 
> I "cheated" and made an interface for enum types:

(Why is this cheating?)

> 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

I've been using a similar technique, but it seems to me that there's
no reason to invent those "test.a" strings and store them in your Enum.
Why should the Enum have to know about the structure of resource
bundles, which may be authored by different folks to the Java
developers? So I use the Enum's name (A and B in the example above)
with a method like this:

public String getBundleKey() {
   return name();
}

Or you could use the class name + enum name if you're worried about
name clashes. Then the Enum doesn't need to know anything about the
view layer.


Jonathan
-- 
.....................................................................
           Dr Jonathan Harley   .
                                .   Email: jon@parkplatz.net
            Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
            www.parkplatz.net   .   Mobile: 079 4116 0423


Re: AW: Best Practice - Converting raw property values of Beans

Posted by Jonathan Harley <jo...@parkplatz.net>.
Bieringer Dominik wrote:
> Hey cool... thx very much... I didn't knew about the possibility to access
> the message from the bundle via 
> 
> <t:outputText value="#{msg[exampleBean.testEnum.bundleKey]}" />
> 
> That makes life easier.

It does, but you still have to declare the message bundle for "msg".
There's a technique I like that simplifies things even further, which
is to use an EL function to do the bundle lookup - then you don't even
have to declare the resource bundle, just the function namespace, which
is the same on every page. That makes it easy to reorganise and rename
message bundles without having to change every page.

> -----Ursprüngliche Nachricht-----
> Von: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com] 
> Gesendet: Mittwoch, 25. Oktober 2006 22:30
> An: MyFaces Discussion
> Betreff: Re: Best Practice - Converting raw property values of Beans
> 
> I "cheated" and made an interface for enum types:

(Why is this cheating?)

> 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

I've been using a similar technique, but it seems to me that there's
no reason to invent those "test.a" strings and store them in your Enum.
Why should the Enum have to know about the structure of resource
bundles, which may be authored by different folks to the Java
developers? So I use the Enum's name (A and B in the example above)
with a method like this:

public String getBundleKey() {
   return name();
}

Or you could use the class name + enum name if you're worried about
name clashes. Then the Enum doesn't need to know anything about the
view layer.


Jonathan
-- 
.....................................................................
           Dr Jonathan Harley   .
                                .   Email: jon@parkplatz.net
            Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
            www.parkplatz.net   .   Mobile: 079 4116 0423

AW: Best Practice - Converting raw property values of Beans

Posted by Bieringer Dominik <Bi...@gmx.at>.
Hey cool... thx very much... I didn't knew about the possibility to access
the message from the bundle via 

<t:outputText value="#{msg[exampleBean.testEnum.bundleKey]}" />

That makes life easier.
Thx

-----Ursprüngliche Nachricht-----
Von: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com] 
Gesendet: Mittwoch, 25. Oktober 2006 22:30
An: MyFaces Discussion
Betreff: Re: Best Practice - Converting raw property values of Beans

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 <Bi...@gmx.at> 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


Re: Best Practice - Converting raw property values of Beans

Posted by Andrew Robinson <an...@gmail.com>.
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 <Bi...@gmx.at> 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

Re: Best Practice - Converting raw property values of Beans

Posted by Harry Co <ha...@gmail.com>.
Hi Dominik,

Sorry to answer your post so lately... I'm noot as present as I would like
on the forum.
So correct me if I misunsterstood your demand:
1./ Your bean tells you the category you want to display a message for
2./ Your messages are in bundle
3./ You want to  display the proper message accoording to the catecory.

So have you simply tried this?

<h:outputText value="#{Msg[myBean.categoryId]}"/>

According to your sample, message bundle have to look like this:
CatA=Category A
CatB=Category B
CatC=Category C

instead of

Category_A=Category A
Category_B=Category B
Category_C=Category C

You also need all possible values to be bound in the bundle to avoid
receiving odd missingResource message.

HTH

Harry,


2006/10/25, Bieringer Dominik <Bi...@gmx.at>:

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