Bob Lee <[EMAIL PROTECTED]> wrote:
> My project needs to pull properties from a database rather than a 
> properties file. I have a couple of questions after reading this section 
> in the manual:
> 
> 
http://jakarta.apache.org/struts/userGuide/configuration.html#resources_config
> 
> 1) How do I specify which bundle to pull from in my JSPs and actions?

In struts.config.xml:

   <message-resources 
                factory="xxx.yyy.struts.utils.DatabaseMessageResourcesFactory"
         parameter="java.resources.application" />


> 2) Do I implement a custom factory or configuration bean that hits the 
> database rather than a properties file?
> 
> 3) Is there already code that does this?

Yeah, in its simplest form, it can be something like the following (which 
reads from an alternate class if available, then tries the regular propery 
file, then just outputs the key).  [Simplified work based on James 
Mitchell's DBMessageResources].


public class DatabaseMessageResourcesFactory extends 
PropertyMessageResourcesFactory {
    public MessageResources createResources(String config) {
        return new DatabaseMessageResources(this, config, this.returnNull);
    }
}

public class DatabaseMessageResources extends PropertyMessageResources 
implements Serializable
{
        public DatabaseMessageResources(MessageResourcesFactory factory, String 
config) {
                this(factory, config, false);
        }
        public DatabaseMessageResources(MessageResourcesFactory factory, String 
config, boolean returnNull) {
                super(factory, config, returnNull);
        }
        public String getMessage(Locale locale, String key) {
                // First get the key from our database
                String aMessage = Localizer.getInstance().getMessage(locale, key);
                if (null != aMessage)  return aMessage;
                
                // If this fails, get the key from properties file as backup
                aMessage = super.getMessage(locale, key);
                if (null != aMessage)  return aMessage;
                
                // If not found anywhere, just show key as is.
            return key;
        }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to