用另一个文本编程替换“选择一个”
问题描述:
我需要用另一个文本以编程方式替换DropDownChoice
中的“选择一个”文本。 (即,我不能将替换文本放在.properties文件中,建议使用here。)我该如何实现这一目标?用另一个文本编程替换“选择一个”
给一点背景,我有对象,看起来大致是
FruitOption
"No fruit chosen"
Orange
Banana
AnimalOption
"No animal chosen"
Dog
Cat
和"No _____ chosen"
串是选择的对象的一部分,并从数据库加载。
我意识到我可以使用空对象模式,并在ChoiceRenderer中给予null对象一个特殊的处理,但我不想因为选择对象是一个抽象类型而不便于创建一个虚拟对象对象为。
答
所有以下面向NULL的方法在AbstractSingleSelectChoice
(参见the online JavaDoc)中声明,它是超类DropDownChoice
。您可以在组件中定义任何相关的String
值,或使用基于属性的格式化消息。回顾了解它们如何工作的方法,然后用适合您需求的任何方法替换示例实现:
/**
* Returns the display value for the null value.
* The default behavior is to look the value up by
* using the key retrieved by calling: <code>getNullValidKey()</code>.
*
* @return The value to display for null
*/
protected String getNullValidDisplayValue() {
String option =
getLocalizer().getStringIgnoreSettings(getNullValidKey(), this, null, null);
if (Strings.isEmpty(option)) {
option = getLocalizer().getString("nullValid", this, "");
}
return option;
}
/**
* Return the localization key for the nullValid value
*
* @return getId() + ".nullValid"
*/
protected String getNullValidKey() {
return getId() + ".nullValid";
}
/**
* Returns the display value if null is not valid but is selected.
* The default behavior is to look the value up by using the key
* retrieved by calling: <code>getNullKey()</code>.
*
* @return The value to display if null is not valid but is
* selected, e.g. "Choose One"
*/
protected String getNullKeyDisplayValue() {
String option =
getLocalizer().getStringIgnoreSettings(getNullKey(), this, null, null);
if (Strings.isEmpty(option)) {
option = getLocalizer().getString("null", this, CHOOSE_ONE);
}
return option;
}
/**
* Return the localization key for null value
*
* @return getId() + ".null"
*/
protected String getNullKey() {
return getId() + ".null";
}
优秀。谢谢! – aioobe 2015-02-08 22:24:33