以编程方式将 "Choose One" 替换为另一个文本
Replace "Choose One" with another text programmatically
我需要以编程方式将 DropDownChoice
中的 "Choose One" 文本替换为另一个文本。 (即,我不能按照建议将替换文本放入 .properties 文件中 here。)我该如何实现?
为了提供一些背景信息,我有一些对象看起来大致像
FruitOption
"No fruit chosen"
Orange
Banana
AnimalOption
"No animal chosen"
Dog
Cat
并且 "No _____ chosen"
字符串是选项对象的一部分并从数据库加载。
我知道我可以使用空对象模式,并在 ChoiceRenderer 中对空对象进行特殊处理,但我不想这样做,因为选择对象属于抽象类型,不方便创建
的虚拟对象
以下所有面向 NULL 的方法都声明在:AbstractSingleSelectChoice
(参见 the online JavaDoc),它是 DropDownChoice
的超 class。您可以在组件中定义任何相关的 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";
}
我需要以编程方式将 DropDownChoice
中的 "Choose One" 文本替换为另一个文本。 (即,我不能按照建议将替换文本放入 .properties 文件中 here。)我该如何实现?
为了提供一些背景信息,我有一些对象看起来大致像
FruitOption
"No fruit chosen"
Orange
Banana
AnimalOption
"No animal chosen"
Dog
Cat
并且 "No _____ chosen"
字符串是选项对象的一部分并从数据库加载。
我知道我可以使用空对象模式,并在 ChoiceRenderer 中对空对象进行特殊处理,但我不想这样做,因为选择对象属于抽象类型,不方便创建
的虚拟对象以下所有面向 NULL 的方法都声明在:AbstractSingleSelectChoice
(参见 the online JavaDoc),它是 DropDownChoice
的超 class。您可以在组件中定义任何相关的 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";
}