You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Todd G. Nist" <tn...@bellsouth.net> on 2002/02/06 15:01:35 UTC

PropertyMessageResourcesFactory Question

I have a few questions on the creation of a custom MessageResourcesFactory
which I am hoping someone can help answer.  We have a database which
contains most of the translation string for our application, so I would like
to create the MessageResources from here in addition to the standard
ApplicationResource.properties file.  I have written my own
PropertyMessageResourcesFactory, modified my web.xml so that Struts would
use this factory, and extended the ActionServlet to call a method to load
the data from the database.  I thought this would provide a simple yet
elegant solution because nothing else would need to change for it to work.

My questions:

1.	While it appears that my code is adding the content of the tables to the
messages HashMap, when I go to reference them with a <bean:message
key="MLSBuyer.title"> I get the following exception:

	Missing message for key
	MLSBuyer.title' javax.servlet.jsp.JspException: Missing message for key
	MLSBuyer.title at
	org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:298) at
	org.apache.jsp.CompanyZoomBody$jsp._jspService(CompanyZoomBody$jsp.java:84)
at ...

I have not implemented the getMessage(Locale locale, String key) method in
my custom PropertyMessageResources class, I was hoping that the messages
that I created would become part of the default bundle/messages hashmap and
therefore would be available to the Struts frame work.  This does no appear
to be what is happening.  Is there a specific Servlet context attribute or
some other value which needs to be set for these messages to be included in
the standard bundle?  What is the difference between Action.MESSAGE_KEY and
Action.MESSAGES_KEY?

2.	What else needs to be done to make these available in the default bundle
so that the Struts frame work can use them without us having to implement
the getMessages() methods? Or can this be done?

3.	Are there any major issues/drawbacks with using the database like this
for building the messages HashMap?  We will still use the
ApplicationResources.properties file where appropriate, but since the
database already has the other translation information in it, it seems
logical to use it, but I am open for other views on this.

I have attached the MessageResourcesFactory, DBMessageResourcesFactory and
the actual class which is created from within the factory,
BasePropertyMessageResources.  Below is the initApplication() method which
we have added to the extended ActionServlet to invoke the method "dbLoad()"
in the BasePropertyMessageResources class.

Thanks in advance.

Todd G. Nist
Email:   tnist@fbos.com

// initApplicationn method of the extended ActionServlet

    public void initApplication() throws ServletException {
        super.initApplication();

        String dataSource =
getServletConfig().getInitParameter("datasource");

        MessageResources mr =

(MessageResources)getServletContext().getAttribute(Action.MESSAGES_KEY);

        if (mr == null) {
            System.out.println("Message Resource is invalid....");
            return;
        }
	  // load resources from the dataSource
        ((BasePropertyMessageResources)mr).dbLoad(dataSource);
    }