Modify Struts 2, behavior of i18n classes when key is not found

We used i18n getText

in actions, setMessageKey

validators and <s:text>

jsp files for the application.

When Struts 2 cannot find the key in the resource packs, it returns the key itself. For example form.transfer.confirm

.

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

+3


source to share


1 answer


You need to create your own implementation TextProvider

and override the methods getText

.

1) Create a class (for example EmptyDefaultTextProvider

) extending one of the TextProvider

existing implementations (for example TextProviderSupport

).

2) Override all methods getText

:



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

      

3) Use your own class as the default text provider. Place below in struts.xml.

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

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

      

+3


source







All Articles