Migrate from Struts 2.3.33 to Struts 2.5.12 LocalizedTextUtil is removed

前端 未结 2 1712
天命终不由人
天命终不由人 2021-01-26 00:41

We tried to migrate from Struts 2.33 to Struts 2.5.12.

We used LocalizedTextUtil in our application directly.

I found that the LocalizedTextU

相关标签:
2条回答
  • 2021-01-26 01:21

    The struts.xml should be set as:

    <constant name="struts.xworkTextProvider" value="DefaultTextProvider" />
    <bean type="com.opensymphony.xwork2.TextProvider" name="DefaultTextProvider" class="utils.CustomTextProvider" scope="default" />
    

    And CustomStrutsTextProviderFactory could add bundles

    public class MyTextProviderFactory implements TextProviderFactory {
    
    protected LocaleProviderFactory localeProviderFactory;
    protected LocalizedTextProvider localizedTextProvider;
    
    @Inject
    public MyTextProviderFactory(LocaleProviderFactory localeProviderFactory, LocalizedTextProvider localizedTextProvider) {
        this.localeProviderFactory = localeProviderFactory;
        this.localizedTextProvider = localizedTextProvider;
    
        this.localizedTextProvider.addDefaultResourceBundle("messages/label");
        this.localizedTextProvider.addDefaultResourceBundle("messages/customerA/label");
    
    }
    
    @Override
    public TextProvider createInstance(Class clazz) {
        TextProvider instance = getTextProvider(clazz);
        if (instance instanceof ResourceBundleTextProvider) {
            ((ResourceBundleTextProvider) instance).setClazz(clazz);
            ((ResourceBundleTextProvider) instance).setLocaleProvider(localeProviderFactory.createLocaleProvider());
        }
        return instance;
    }
    
    @Override
    public TextProvider createInstance(ResourceBundle bundle) {
        TextProvider instance = getTextProvider(bundle);
        if (instance instanceof ResourceBundleTextProvider) {
            ((ResourceBundleTextProvider) instance).setBundle(bundle);
            ((ResourceBundleTextProvider) instance).setLocaleProvider(localeProviderFactory.createLocaleProvider());
        }
        return instance;
    }
    
    protected TextProvider getTextProvider(Class clazz) {
        return new TextProviderSupport(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
    }
    
    protected TextProvider getTextProvider(ResourceBundle bundle) {
        return new TextProviderSupport(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
    }
    

    }

    Complete credits to @LukazLenart https://issues.apache.org/jira/browse/WW-4829

    0 讨论(0)
  • 2021-01-26 01:26

    You should use StrutsLocalizedTextProvider or GlobalLocalizedTextProvider. They have some util api, however it's marked as deprecated. Happily, it's documented in the javadoc.

    I'm referencing source code from the repo because I was unable to find these classes in javadoc for Struts 2 Core 2.5.11-SNAPSHOT API

    0 讨论(0)
提交回复
热议问题