This looks interesting. I've been to implement a similar approach using a dummy 
Map that builds the key. Although I think your solution looks even nicer than 
what I came up with. I'll have to give it try.

 

Thanks

 

________________________________

From: Michael Heinen [mailto:[email protected]] 
Sent: vendredi 6 mars 2009 11:54
To: MyFaces Discussion
Subject: RE: Message bundle problem

 

You can use a Lazy map as Wrapper to build the keys dynamically.

 

A Transformer will be called during map access and you can add your prefix to 
your key.

This does also work inside tables, lists etc

 

Sample:

import java.io.Serializable;

import java.util.HashMap;

import java.util.Map;

import org.apache.commons.collections.Transformer;

import org.apache.commons.collections.map.LazyMap;

 

public class MessageKeyCreatorBean implements Serializable

{

  private Map mLazyMap;

  public MessageKeyCreatorBean(){}

 

  public void setTransformer (Object input)

  {

    String inputString = (String) input; 

    Transformer keyTransformer = new KeyTransformer(inputString);

    this.mLazyMap = LazyMap.decorate(new HashMap(), keyTransformer);

  }

 

  public Map getLazyKey()

  {

    return this.mLazyMap;

  }

  

  public Object get(Object key)

  {

    return this.mLazyMap.get(key);

  } 

  

  private static class KeyTransformer implements Transformer, Serializable

  {

    private String mPrefix = null;

 

    public KeyTransformer(String prefix )

    {

      this.mPrefix = prefix;

    }

    public Object transform(Object input)

    {

      return this.mPrefix+input.toString();

    }

  }

}

Config:

  <managed-bean>

    <managed-bean-name>myPrefixBean</managed-bean-name>

    <managed-bean-class>....MessageKeyCreatorBean</managed-bean-class>

    <managed-bean-scope>application</managed-bean-scope>

    <managed-property>

      <property-name>transformer</property-name>

      <value>linked</value>

    </managed-property>

  </managed-bean>

 

View:

myPrefixBean.lazyKey[bean.type]

 

Michael

 

From: [email protected] 
[mailto:[email protected]] 
Sent: Freitag, 6. März 2009 11:31
To: [email protected]
Subject: Message bundle problem

 

Hi,

 

I need to access a message bundle property like so:

 

<t:outputText value="#{msg['linked.' + bean.type]}" />

 

Although that syntax doesn't quite work. I've tried a few things. The closest I 
got was using a t:aliasBean like

 

<t:aliasBean alias="#{key}" value="linked.#{bean.type}" />

 

<t:outputText value="#{msg[key]}" />

 

This actually works, but problems arise when you stick that into a t:dataList. 
Inside the datalist the t:aliasBean doesn't get reevaluated and instead the 
property resolver will try and resolve "#{msg[linked.#{bean.type}]} which 
doesn't work.

 

I had a temporary fix which changed the value stored in the bean to include the 
"linked." part but that causes problems later with the application logic. 

 

Thanks for any help

Matt

Reply via email to