You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Da...@kp.org on 2005/06/16 21:03:19 UTC
Using Validator and getting MessageResource from a database
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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Using Validator and getting MessageResource from a database
Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
My mistake, the plumbing is in the DTD to do this (i.e. a className for a
custom MessageResourcesConfig and the <set-property> element) but the
digester rules always create the standard MessageResourcesConfig and ignore
any custom implementations specified in the className.
However, even if that was fixed - the factory is only given access to the
parameter and not the MessageResourcesConfig - so if you did set some custom
properties, there isn't a straightforward way to access them.
One way you could do this would be to specify the name of a properties file
in the "parameter" - then in your factory read the properties from there.
Sorry, I assumed that since the DTD provided for setting properties - the
mechanism would be in there to make it work. Unfortunately its not :-(
Niall
----- Original Message -----
From: <Da...@kp.org>
Sent: Friday, June 17, 2005 1:56 AM
> Thanks Niall. If I were to use <set-property> elements, do you have any
> idea how I would retrieve those properties within the factory?
>
> Thanks,
> David
>
> "Niall Pemberton" <ni...@blueyonder.co.uk> wrote on 06/16/2005
> 04:03:36 PM:
>
> > You need to configure your message resources through the
> struts-config.xml -
> > init parameters in the web.xml don't work in Struts 1.2
> >
> > http://struts.apache.org/userGuide/configuration.html#resources_config
> >
> > So in your case you need something like the following in your
> > struts-config.xml....
> >
> > <message-resources parameter="..."
> > factory="org.example.MyMessageResourceFactory " />
> >
> > You can use the "parameter" element for any configuration value you want
> -
> > thats the argument passed to the factory's createResources() method. I
> > believe you can also use <set-property> elements for other config values
> -
> > although I've never actually tried it out.
> >
> > Niall
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Using Validator and getting MessageResource from a database
Posted by Da...@kp.org.
Thanks Niall. If I were to use <set-property> elements, do you have any
idea how I would retrieve those properties within the factory?
Thanks,
David
"Niall Pemberton" <ni...@blueyonder.co.uk> wrote on 06/16/2005
04:03:36 PM:
> You need to configure your message resources through the
struts-config.xml -
> init parameters in the web.xml don't work in Struts 1.2
>
> http://struts.apache.org/userGuide/configuration.html#resources_config
>
> So in your case you need something like the following in your
> struts-config.xml....
>
> <message-resources parameter="..."
> factory="org.example.MyMessageResourceFactory " />
>
> You can use the "parameter" element for any configuration value you want
-
> thats the argument passed to the factory's createResources() method. I
> believe you can also use <set-property> elements for other config values
-
> although I've never actually tried it out.
>
> Niall
>
> ----- Original Message -----
> From: <Da...@kp.org>
> Sent: Thursday, June 16, 2005 8:03 PM
>
>
> > 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.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Using Validator and getting MessageResource from a database
Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
You need to configure your message resources through the struts-config.xml -
init parameters in the web.xml don't work in Struts 1.2
http://struts.apache.org/userGuide/configuration.html#resources_config
So in your case you need something like the following in your
struts-config.xml....
<message-resources parameter="..."
factory="org.example.MyMessageResourceFactory " />
You can use the "parameter" element for any configuration value you want -
thats the argument passed to the factory's createResources() method. I
believe you can also use <set-property> elements for other config values -
although I've never actually tried it out.
Niall
----- Original Message -----
From: <Da...@kp.org>
Sent: Thursday, June 16, 2005 8:03 PM
> 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.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Using Validator and getting MessageResource from a database
Posted by Da...@kp.org.
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: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org