在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。
当我提出一些断点时,我的方法都没有被调用,而且我的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
请告诉我如何处理它?!
答案 0 :(得分:1)
引起:Bean类型接口com.opensymphony.xwork2.LocalizedTextProvider,名称为struts已经加载
你不应该加载具有相同接口的bean两次。如果你想获得容器加载的bean的实例,你应该使用DI。由于DI没有记录并且Struts不支持,我不建议您使用它。
如果您需要自定义文字提供者,可以按should point to a certificate bundle答案中的说明创建自己的文字提供者。
您可以创建自己的文本提供程序并将其注册
<constant name="struts.xworkTextProvider" value="util.CustomTextProvider"/>
:class LoadingSlip(models.Model): _name = 'loading.slip' _description = 'loading information' partner_id = fields.Char("Customer Name") order_date = fields.Date("Order Date") expiration_date = fields.Date("Expiration Date") # order_line = fields.One2many('sale.order.line', 'order_id', string="Order Lines") product_line = fields.One2many('loading.product.line', 'loading_product', string="Loading Products") class LoadingProduct(models.Model): _name = 'loading.product.line' _description = "Loading Product Informations" products_id = fields.Many2one('product.product', string='Product', ondelete='restrict', index=True) quantity = fields.Float(string='Quantity', default=1) loading_product = fields.Many2one('loading.slip', string="Loading Reference", ondelete='cascade', index='True')
答案 1 :(得分: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
}