Change Struts 2, i18n classes behavior when key is

2019-02-25 04:08发布

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条回答
Lonely孤独者°
2楼-- · 2019-02-25 04:23

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" />
查看更多
登录 后发表回答