2015-02-07 93 views
2

我需要用另一個文本以編程方式替換DropDownChoice中的「選擇一個」文本。 (即,我不能將替換文本放在.properties文件中,建議使用here。)我該如何實現這一目標?用另一個文本編程替換「選擇一個」

給一點背景,我有對象,看起來大致是

FruitOption 
    "No fruit chosen" 
    Orange 
    Banana 

AnimalOption 
    "No animal chosen" 
    Dog 
    Cat 

"No _____ chosen"串是選擇的對象的一部分,並從數據庫加載。

我意識到我可以使用空對象模式,並在ChoiceRenderer中給予null對象一個特殊的處理,但我不想因爲選擇對象是一個抽象類型而不便於創建一個虛擬對象對象爲。

回答

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"; 
} 
+0

優秀。謝謝! – aioobe 2015-02-08 22:24:33