You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Roland Huss <Ro...@consol.de> on 2001/02/14 22:08:43 UTC

MessagesResources - some thoughts

Hi,

for some reasons I would like to separate messages put into JSP-pages
with <bean:message> from messages generated within java
code. I.e. packaging your application as jar file and still give
designers a change to customize messages (without touching the jars)
seems to be a good  idea (at least for me ;-)

Hence I'v written a MessageResources subclass, which fetches the
properties from a file somewhere below the serlvet context. However,
there are some problems with the current design of the MessageResource
handling of struts:

  * Theres no easy way to pass extra parameters to the
    MessageResourcesFactory when creating the application resources. 

  * PropertyMessageResources is hard to subclass

==================================================

My solution for now  is to subclass
PropertyMessageResources and to duplicate the loadLocale() method
nearly completely except for obtaining the properties' InputStream:


  URL resource = servletCtx.getResource(name);
  is = resource.openConnection().getInputStream();

instead of  
 
  is = this.getClass().getClassLoader().getResourceAsStream(name);

(serlvetCtx is the ServletContext obtained from my own
MessageResourcesFactory)

Furthermore I have to use a subclassed ActionServlet
which duplicates nearly everything from 'initApplication()' except
for: 

  .......
  MessageResourcesFactory factoryObject =
                MessageResourcesFactory.createFactory();

  // Set servlet context to our factory (nasty...)
  if (factoryObject instanceof CtxMessageResourcesFactory)
  {
     ( (CtxMessageResourcesFactory) factoryObject).setServletCtx(
           getServletConfig().getServletContext());
  }
  .......
===================================================

My suggestion is to extract the creation of the  InputStream in
PropertyMessageResources into an extra method like

 protected InputStream getInputStream(String name)
 {
    return this.getClass().getClassLoader().getResourceAsStream(name);
 }

so that it can be subclassed without duplicating the caching stuff.

For the problem with passing extra parameters to a
MessageResourcesFactory I don't see any solution. Except maybe (at
least for my case) to include a ServletContext attribute directly into
the base MessageResources class, which is maybe not so a bad idea in
general.  Or even a protected initMessageResourcesFactory() (which
does nothing in the default case) in ActionServlet would be of much
help.

Are there any chance for getting this or something similar into the
mainstream struts cvs tree ?

cu...
-- 
							...roland huss
						             consol.de


Re: MessagesResources - some thoughts

Posted by ma...@tumbleweed.com.
Isn't this essentially why the "bundle" attribute was added to the 
<bean:message> tag? That would seem to solve the problem, although I guess 
it means you'd have to add that attribute to all your <bean:message> instances.

--
Martin Cooper
Tumbleweed Communications


At 09:41 AM 2/16/01 -0600, Craig Tataryn wrote:
>But technically couldn't the application developer do this already?  They
>could just add another init-param to the Servlet which specified a
>Resources class other than the default.  The servlet writter would simply
>use this resource bundle instead, getting it's messages from there.
>
>You could have something like UIResource and ServletResource classes.  The
>UIResource class would be the one you set in the ActionServlet's
>application init-param, and the ServletResource class can be loaded
>programatically by the servlet writter based on some custom init-param
>which specified the class name.
>
>Craig T.
>
>Rob Leland wrote:
>
> > Roland Huss wrote:
> > >
> > > Hi,
> > >
> > > for some reasons I would like to separate messages put into JSP-pages
> > > with <bean:message> from messages generated within java
> > > code.
> > +1
> > This sounds like a good Idea. I would vote for this change.
> > Maybe you could post it to the bug/feature request so it
> > ends up one the list for version 1.1.
> >
> > -Rob
>
>--
>I've been trying to change the world for years, but they just won't give me
>the source code....
>
>



Re: MessagesResources - some thoughts

Posted by Roland Huss <Ro...@consol.de>.
Hello,

Craig Tataryn wrote:

 > But technically couldn't the application developer do this already?  They
 > could just add another init-param to the Servlet which specified a
 > Resources class other than the default.  The servlet writter would simply
 > use this resource bundle instead, getting it's messages from there.

The problem with the init-param 'factory', which you can use for
setting a MessagerResourcesFactory for use of your application
MessageResources (i.e. those which you access with your
<bean:message/> tags by default), is, that you can't set any extra
parameters for you own factory. This is no a serious problem by its
own, since you can always subclass ActionServlet and override
'initApplication()'. The point is, that at the moment, you need to
duplicate a lot of code from ActionServlet.initApplication() only for
setting some extra parameter (the suggestion was to introduce an extra
protected method in ActionServlet like 'initMessageResourcesFactory()'
to make subclassing easier). 

 > You could have something like UIResource and ServletResource classes.  The
 > UIResource class would be the one you set in the ActionServlet's
 > application init-param, and the ServletResource class can be loaded
 > programatically by the servlet writter based on some custom init-param
 > which specified the class name.

Martin Cooper wrote:

 > Isn't this essentially why the "bundle" attribute was added to the 
 > <bean:message> tag? That would seem to solve the problem, although I guess 
 > it means you'd have to add that attribute to all your <bean:message> instances.

You are right, this is a solution if you want to switch to different
MessageResources. But this is tedious, if you really only want to set
a new default application ResourceMessages.

Anyway, I think that an alternate MessageResources which get its messages from a
file somewhere below the servlet context would be a good idea for the
main struts development branch.....
-- 
							...roland huss
						             consol.de


Re: MessagesResources - some thoughts

Posted by Craig Tataryn <Cr...@msdw.com>.
But technically couldn't the application developer do this already?  They
could just add another init-param to the Servlet which specified a
Resources class other than the default.  The servlet writter would simply
use this resource bundle instead, getting it's messages from there.

You could have something like UIResource and ServletResource classes.  The
UIResource class would be the one you set in the ActionServlet's
application init-param, and the ServletResource class can be loaded
programatically by the servlet writter based on some custom init-param
which specified the class name.

Craig T.

Rob Leland wrote:

> Roland Huss wrote:
> >
> > Hi,
> >
> > for some reasons I would like to separate messages put into JSP-pages
> > with <bean:message> from messages generated within java
> > code.
> +1
> This sounds like a good Idea. I would vote for this change.
> Maybe you could post it to the bug/feature request so it
> ends up one the list for version 1.1.
>
> -Rob

--
I've been trying to change the world for years, but they just won't give me
the source code....


Re: MessagesResources - some thoughts

Posted by Rob Leland <Ro...@freetocreate.org>.

Roland Huss wrote:
> 
> Hi,
> 
> for some reasons I would like to separate messages put into JSP-pages
> with <bean:message> from messages generated within java
> code. 
+1
This sounds like a good Idea. I would vote for this change.
Maybe you could post it to the bug/feature request so it
ends up one the list for version 1.1.

-Rob