Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strust 2.15 How to override TextProviderSupport to customize resource bundel mesages

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 ?!

like image 569
Alireza Fattahi Avatar asked Jan 25 '26 15:01

Alireza Fattahi


1 Answers

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"/>
like image 187
Roman C Avatar answered Jan 27 '26 05:01

Roman C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!