2017-07-29 18 views
2

在Struts 2.3,覆蓋我們struts.xml中使用下面2.15 STRUST如何重寫TextProviderSupport定製資源bundel mesages

集豆的TextProvider

<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。

當我放了一些斷點時,我的方法都沒有被調用,看起來我的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 

能否請你讓我知道該如何處理呢?!

回答

1

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

您不應該加載具有相同接口的bean兩次。如果你想獲得容器加載的bean的實例,你應該使用DI。由於DI沒有文檔記錄,並且Struts不支持,所以我不建議您使用它。


如果您需要自定義文本提供,你可以創建自己的文本提供在this答案解釋。

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

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

親愛的@RomanC你的答案似乎是完整的,我已經檢查http://struts.apache.org/maven/struts2-core/apidocs/com/opensymphon y/...,這證明了這種方式。但升級到2.5.12後它不起作用。爲了做一些測試,我從'strust.xml'中刪除了我的bean,並在'DefaultTextProvider'中放置了一些斷點,這個類的'getText'沒有被調用,但是我發現調用了'TextProviderSupport'' getText'方法。任何想法?我們應該重寫另一個課程嗎?! –

+0

是的,閱讀提供給這個答案的鏈接。 –

0

首先,你需要定義自定義工廠:

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

然後,加載您CustomTextProvider類:

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