I found the answer I was looking for. The solution is to set the "factory"
attribute of the "message-resource" element in struts-config, like so:
<message-resources
parameter="MyMessageResources"
factory="my.example.MyMessageResourceFactory">
</message-resources>
> I have a project where I want to use the Struts Validator. By default
the
> error messages are read from a properties file. However I'm required to
> read the error messages from a database. The question is how do I do
this?
> I found this article
> http://www.informit.com/articles/article.asp?p=101174&rl=1, from the
> "Struts Kick Start" book, which shows how to do this but it doesn't seem
> to work.
>
> Here is what I did. First I created a MessageResourcesFactory that
creates
> my version of MessageResources:
>
> import org.apache.struts.util.MessageResources;
> import org.apache.struts.util.MessageResourcesFactory;
>
> public class MyMessageResourceFactory extends MessageResourcesFactory {
>
> public MessageResources createResources(String arg0) {
> return new DbMessageResources(this, arg0);
> }
> }
>
> Next I created my version of MessageResources:
>
> import java.util.Locale;
> import org.apache.struts.util.MessageResources;
> import org.apache.struts.util.MessageResourcesFactory;
>
> public class DbMessageResources extends MessageResources implements
> java.io.Serializable
> {
>
> public MessageResource(MessageResourcesFactory arg0, String
arg1)
> {
> super(arg0, arg1);
> }
>
> public String getMessage(Locale arg0, String key) {
> // based on locale and key, retrieve message from
database
> // for this example just return the key
> return key;
> }
>
> }
>
> Lastly, the article says to add these parameters ("application" and
> "factory") to the ActionServlet config in web.xml file to tell Struts to
> use my implementation.
>
> <servlet>
> <servlet-name>action</servlet-name>
> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
> <init-param>
> <param-name>config</param-name>
> <param-value>/WEB-INF/struts-config.xml</param-value>
> </init-param>
> <param-name>application</param-name>
> <param-value>org.example.DbMessageResources</param-value>
> </init-param>
> <init-param>
> <param-name>factory</param-name>
> <param-value>org.example.MyMessageResourceFactory </param-value>
> </init-param>
> <load-on-startup>2</load-on-startup>
> </servlet>
>
> Trouble is this code never gets called. I'm on Struts 1.2.7. Is this the
> right way to get Struts to use my version of MessageResources, or is
there
> a different way to do this now? I've searched for more documentation
> regarding reading resources from the database, but haven't found much.
Any
> hints, tips, or references would be greatly appreciated.
>
> Thanks,
> David
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]