struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Wallace <rwall...@thewallacepack.net>
Subject [shale] Add getMessage() method to AbstractViewController?
Date Tue, 21 Feb 2006 17:28:30 GMT
Hello everyone,

I have a simple feature request.  Can we get a method added to the 
AbstractViewController class that gets a message out of the applications 
configured message bundle?  Something as simple as:

    protected String getMessage (String messageKey) {
        ResourceBundle rb = ResourceBundle.getBundle (getApplication 
().getMessageBundle (), getFacesContext ().getViewRoot ().getLocale ());
        return rb.getString (messageKey);
    }

Something that might also be useful is one that takes an optional 
Object[] as parameters and then uses MessageFormat to format the message:

    protected String getMessage (String messageKey, Object[] params) {
        String messageBundleName = getApplication ().getMessageBundle ();
        Locale locale = getFacesContext ().getViewRoot ().getLocale ();
        ResourceBundle rb = ResourceBundle.getBundle (messageBundleName, 
locale);
        String msgPattern = rb.getString (messageKey);
        String message;
        if (params != null) {
            message = MessageFormat.format (msgPattern, params);
        } else {
            message = msgPattern;
        }
        return message;
    }


I've found this to be of tremendous help in simplifying error handling 
code, or when I want to display a success message or something like 
that.  It makes things so much cleaner.


What do you think?

Thanks,
Rich

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message