2

In struts 2.3 , to override the TextProvider we used below

Set bean in struts.xml:

<bean type="com.opensymphony.xwork2.TextProvider" name="DefaultTextProvider" class="util.CustomTextProvider" scope="default" />

And make CustomTextProvider

public class CustomTextProvider extends DefaultTextProvider{

public String getText(String key, String defaultValue, List<?> args) {
        String text = super.getText(key, defaultValue, args);
        //Do something with the text
        //and return it
    }

 //other getText methods can be override too
}

This seems not to be worked at Struts 2.15.2.

When I put some break points none of my methods are called and it seems that my bean is not registered.

I thought that the StrutsLocalizedTextProvider may be the one to be override. But It seems that I can not define a bean which extends it. I get this error:

Unable to load configuration. - bean - file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/WEB-INF/classes/struts.xml:12:156
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
    at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:960)
    at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:466)
    at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:499)
    at org.apache.struts2.dispatcher.InitOperations.initDispatcher(InitOperations.java:75)
    at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:63)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4590)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5233)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: Unable to load bean: type:com.opensymphony.xwork2.LocalizedTextProvider class:utils.CustomLocalizedTextProvider - bean - file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/WEB-INF/classes/struts.xml:12:156
    at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:271)
    at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:98)
    at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:164)
    at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:63)
    ... 17 more
Caused by: Bean type interface com.opensymphony.xwork2.LocalizedTextProvider with the name struts has already been loaded by [unknown location] - bean - file:/E:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/WEB-INF/classes/struts.xml:12:156

Can you please let me know how to handle it ?!

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173

2 Answers2

1

Caused by: Bean type interface com.opensymphony.xwork2.LocalizedTextProvider with the name struts has already been loaded

you shouldn't load a bean with the same interface twice. If you want to get instance of the bean loaded by the container you should use DI. Since DI is not documented and not supported by Struts, I will not recommend you to use it.


If you need a custom text provider, you can create your own text provider as explained in this answer.

You could create your own text provider and register it in struts.xml:

<constant name="struts.xworkTextProvider" value="util.CustomTextProvider"/>
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Dear @RomanC your answer seems to be complete and I have checked it in http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphon‌​y/…, which proves this way. But it does not work after I upgrade to 2.5.12. To make some test, I remove my bean from `strust.xml` and put some break points in `DefaultTextProvider` the `getText` of this class is not called, however I find that the `TextProviderSupport` `getText` method is called. Any Idea? Should we override another class?! – Alireza Fattahi Jul 30 '17 at 04:54
0

First you need to define your custom factory:

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

Then in your factory, load your CustomTextProvider class:

public class CustomStrutsTextProviderFactory extends StrutsTextProviderFactory {

@Override
protected TextProvider getTextProvider(Class clazz) {
    return new CustomTextProvider(clazz, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
}

@Override
protected TextProvider getTextProvider(ResourceBundle bundle) {
    return new CustomTextProvider(bundle, localeProviderFactory.createLocaleProvider(), localizedTextProvider);
  }
}

And finally :

public class CustomTextProvider extends TextProviderSupport {

public CustomTextProvider(Class<?> clazz, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
        super(clazz, provider, localizedTextProvider);
    }

public CustomTextProvider(ResourceBundle bundle, LocaleProvider provider, LocalizedTextProvider localizedTextProvider) {
        super(bundle, provider, localizedTextProvider);
}

public String getText(String key, String defaultValue, List<?> args) {
        String text = super.getText(key, defaultValue, args);
        //Do some thing with text
        //return new customized text;
  }

   //Override other getText if you need

 }

Please refer: https://issues.apache.org/jira/browse/WW-4830

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173