Bill, 

Thanks for the pointer.  I have already downloaded the source and have 
extended Struts successfully many times.  The MessageResources thing was 
something I couldn't quite get my head around, so I figured I'd ask the 
experts.  Now I have the code, it didn't seem so complicated after all.

Cheers,

Alan.

Bill Siggelkow <[EMAIL PROTECTED]> wrote in news:ckjkjn$nqs$1
@sea.gmane.org:

> Alan  -- I strongly encourage you (if you haven't already) to download 
> the Struts source and use it as you develop.  You will be amazed at how 
> much quicker you can grok Struts.
> 
> -Bill Siggelkow
> 
> Alan Pocklington wrote:
> 
>> Oh okay, I'll look into that.  Thanks for your help.
>> 
>> Hubert Rabago <[EMAIL PROTECTED]> wrote in
>> news:[EMAIL PROTECTED]: 
>> 
>> 
>>>Struts has some special treatment for locales.  You'd wanna abide by
>>>that for consistency with what its internal engine uses for the
>>>resource files.
>>>
>>>
>>>On Wed, 13 Oct 2004 15:54:02 +0000 (UTC), Alan Pocklington
>>><[EMAIL PROTECTED]> wrote:
>>>
>>>>Cheers for pointing that out.  As of yet I don't need message
>>>>arguments so that'll be fine.
>>>>
>>>>Was there any reason for having a getLocale(request) method? I'm
>>>>guessing the implementation was different from just
>>>>request.getLocale(). 
>>>>
>>>>
>>>>Hubert Rabago <[EMAIL PROTECTED]> wrote in
>>>>news:[EMAIL PROTECTED]:
>>>>
>>>>
>>>>
>>>>
>>>>>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]
>>>>
>>>>
>>>



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

Reply via email to