Strust 2.15 如何覆盖 TextProviderSupport 以自定义资源包消息

Strust 2.15 How to override TextProviderSupport to customize resource bundel mesages

在 struts 2.3 中,覆盖我们在下面使用的 TextProvider

在 struts.xml 中设置 bean:

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

并使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
}

这似乎不适用于 Struts 2.15.2。

当我放置一些断点时 none 我的方法被调用,似乎我的 bean 没有注册。

我认为 StrutsLocalizedTextProvider 可能是要覆盖的那个。但似乎我无法定义扩展它的 bean。 我收到此错误:

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

你能告诉我如何处理吗?!

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

您不应该使用相同的接口加载一个 bean 两次。如果你想获得容器加载的 bean 实例,你应该使用 DI。由于 DI 没有文档记录并且不受 Struts 支持,因此我不建议您使用它。


如果您需要自定义文本提供程序,您可以创建自己的文本提供程序,如 回答中所述。

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

<constant name="struts.xworkTextProvider" value="util.CustomTextProvider"/>

首先你需要定义你的自定义工厂:

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

然后在您的工厂中,加载您的 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);
  }
}

最后:

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

 }

请参考:https://issues.apache.org/jira/browse/WW-4830