Change Struts 2, i18n classes behavior when key is not found

半世苍凉 提交于 2019-12-04 04:08:13

问题


We used getText in actions, setMessageKey in validators and <s:text> in jsp files for an i18n application.

When Struts 2 could not find a key in resource bundles it returns the key itself. For example form.transfer.confirm.

How can we change this behavior in the way that instead of the key itself Struts2 returns empty string.


回答1:


You need to create custom implementation of TextProvider and override getText methods in it.

1) Create class (e.g. EmptyDefaultTextProvider) extending one of TextProvider existing implementations (e.g. TextProviderSupport).

2) Override all getText methods like that:

public String getText(String key, String defaultValue) {
     return super.getText(key, "");
}

3) Use your custom class as the default text provider. Put below in struts.xml.

<constant name="struts.xworkTextProvider" value="emptyDefaultTextProvider" />

<bean type="com.opensymphony.xwork2.TextProvider" name="emptyDefaultTextProvider" 
                              class="packagepath.EmptyDefaultTextProvider" scope="default" />


来源:https://stackoverflow.com/questions/29946455/change-struts-2-i18n-classes-behavior-when-key-is-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!