以编程方式将“选择一个”替换为另一个文本

问题描述 投票:2回答:1

我需要以编程方式将DropDownChoice中的“Choose One”文本替换为另一个文本。 (即,我不能将替换文本放在.properties文件中,如here所示。)我如何实现这一目标?

为了给出一点上下文,我的对象看起来很像

FruitOption
    "No fruit chosen"
    Orange
    Banana

AnimalOption
    "No animal chosen"
    Dog
    Cat

并且"No _____ chosen"字符串是option-object的一部分并从数据库加载。

我意识到我可以使用null对象模式,并在ChoiceRenderer中给null对象一个特殊的处理,但是我不愿意,因为choice-objects是一个抽象类型,不便于创建一个虚拟对象。

wicket dropdownchoice
1个回答
4
投票

以下所有面向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";
}
© www.soinside.com 2019 - 2024. All rights reserved.