On Wed, 13 Oct 2004 10:38:09 -0500, Hubert Rabago <[EMAIL PROTECTED]> wrote:
> Yup, copy/paste from existing code works fast.  :)

It's also not foolproof.  This excerpt was specific to the needs of
the class I took it from.  Check the getMessage() method -- it can
actually accept message arguments, but the evaluateMessage() doesn't
use them.  If you do need them, just modify evaluateMessage() to also
accept an argument parameter.
Sorry about that.

> 
> On Wed, 13 Oct 2004 15:35:24 +0000 (UTC), Alan Pocklington
> <[EMAIL PROTECTED]> wrote:
> > Wow, thanks for the quick reply. Will do!
> 
> 
> >
> > Hubert Rabago <[EMAIL PROTECTED]> wrote in
> > news:[EMAIL PROTECTED]:
> >
> >
> >
> > > Put this in a util class and call evaluateMessage():
> > >
> > >     /**
> > >      * <p>Evaluate the given message resource key.</p>
> > >      *
> > >      * @param messageKey the name of the resource entry to retrieve
> > >      * @param bundle The key specified in the
> > >      *  <code>&lt;message-resources&gt;</code> element for the
> > >      *  resource bundle to use
> > >      * @param request the request being processed
> > >      * @param servletContext the current [EMAIL PROTECTED] ServletContext}
> > >      * @return the value of paramName from the message resources
> > >      */
> > >     public String evaluateMessage(
> > >             String messageKey,
> > >             String bundle,
> > >             HttpServletRequest request,
> > >             ServletContext servletContext) {
> > >         // get the message resources
> > >         MessageResources resources;
> > >         if (bundle != null) {
> > >             resources = getResources(bundle, request, servletContext);
> > >         } else {
> > >             resources = getResources(request);
> > >         }
> > >         // evaluate the message
> > >         String messageString = getMessage(messageKey, null, request,
> > >                 resources);
> > >
> > >         // add it to the redirect parameters
> > >         if ((messageString != null) && (messageString.length() > 0)) {
> > >             return messageString;
> > >         }
> > >         return null;
> > >     }
> > >
> > >
> > >     /**
> > >      * <p>Look for the ActionMessage in the provided
> > >      MessageResources.</p> *
> > >      * @param messageKey the key to look for
> > >      * @param args the arguments to be used in evaluating the message
> > >      * @param request the request being processed
> > >      * @param resources the application's message resources
> > >      * @return the message formed from the given ActionMessage
> > >      */
> > >     protected String getMessage(String messageKey,
> > >                                 Object[] args,
> > >                                 HttpServletRequest request,
> > >                                 MessageResources resources) {
> > >         if (messageKey == null) {
> > >             //log.warn("Call to getMessage() with a null
> > >             messageKey."); return null;
> > >         }
> > >
> > >         String result = null;
> > >         try {
> > >             // first, get the locale from the request
> > >             Locale userLocale = getLocale(request);
> > >
> > >             // then, get the message
> > >             if (args == null) {
> > >                 result = (resources.getMessage(userLocale,
> > >                 messageKey));
> > >             } else {
> > >                 result = (resources.getMessage(userLocale, messageKey,
> > >                         args));
> > >             }
> > >         } catch (Exception e) {
> > >             //log.error("Exception while looking for message.", e);
> > >         }
> > >         return result;
> > >     }
> > >
> > >     /**
> > >      * <p>Return the default message resources for the current
> > >      module.</p> *
> > >      * @param request The servlet request we are processing
> > >      */
> > >     protected MessageResources getResources(HttpServletRequest
> > >     request) {
> > >
> > >         return ((MessageResources)
> > >                 request.getAttribute(Globals.MESSAGES_KEY));
> > >
> > >     }
> > >
> > >     /**
> > >      * <p>Return the specified message resources for the current
> > >      module.</p> *
> > >      * @param bundle The key specified in the
> > >      *  <code>&lt;message-resources&gt;</code> element for the
> > >      *  requested bundle
> > >      * @param request The servlet request we are processing
> > >      * @param servletContext the current [EMAIL PROTECTED] ServletContext}
> > >      */
> > >     protected MessageResources getResources(String bundle,
> > >                                             HttpServletRequest
> > >                               request,
> > >                                             ServletContext
> > >                               servletContext) {
> > >
> > >         // Identify the current module
> > >         ModuleConfig moduleConfig =
> > >                 ModuleUtils.getInstance().getModuleConfig(
> > >                         request, servletContext);
> > >
> > >         // Return the requested message resources instance
> > >         return ((MessageResources) servletContext.getAttribute
> > >                 (bundle + moduleConfig.getPrefix()));
> > >
> > >     }
> > >
> > >

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

Reply via email to