Pregunta

There are several ways to handle resource bundle using Struts 2.3 framework. This includes using certain UI tags in the View class or getText() method in your Action class.

However, there is no straight-forward mechanism to access this resource bundle in a java class or any other class except View and Action.

I found two ways to access it in a java class,however, they fail to completely replace the old MessageResources of Struts 1.2 framework.

Following are the two ways with drawback of each

  1. Using ResourceBundle class of java.util package.

    Locale locale = (Locale)request.getSession().getAttribute(ConstantsFnl.LOCALE_KEY);
    ResourceBundle rb = ResourceBundle.getBundle(bundle,locale);
    message = rb.getString(key);
    

    The drawback with this approach is you cannot pass an array of arguments which can be replaced in your message text.This functionality was present in Struts 1.2 with MessageReosurces.

  2. Using DefaultTextProvider of Struts 2.3 framework.

    DefaultTextProvider dtp = new DefaultTextProvider();
    return dtp.getText(key, (String[])params);
    

Though this approach gives you a way to pass an array of arguments to be replaced in your message text, it does not give you a mechanism to specify the locale or specify the bundle to be searched.Again,this functionality is present in Struts 1.2 with MessageResources class.

Looking for an optimum approach.Any help on this would be great.

¿Fue útil?

Solución

You can use methods found in:

com.opensymphony.xwork2.util.LocalizedTextUtil
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top