簡體   English   中英

在selectOneMenu中未為空字符串調用JSF Converter

[英]JSF Converter not called for empty String in selectOneMenu

以下示例:

<h:selectOneMenu value="#{bean.value}">
    <f:selectItem itemValue="#{null}" itemDisabled="#{true}" noSelectOption="#{true}" itemLabel="choose one" />
    <f:selectItem itemValue="FEMALE" itemLabel="female" />
    <f:selectItem itemValue="MALE" itemLabel="male" />
    <f:converter converterId="myConverter" />
</h:selectOneMenu>

初始加載時,bean的值為null因此請選擇please-choose-option。 如果用戶什么都不選擇,瀏覽器將不會提交值(禁用所選選項),JSF會將值設置為空String。 如果再次呈現該頁面,則沒有相應的值,JSF將不會呈現選定的屬性,並且大多數瀏覽器會預選擇第一個非禁用值。 壞。

我以為我可以使用Converter輕松地將其更改,該Converter將getAsObject的空String轉換為null 但是在這種情況下,不會調用該轉換器。

有人知道為什么嗎?

我知道我可以使用javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL更改此行為,但這會導致其他字段出現其他問題。

我通過手動設置selected-attribute解決了我的問題:

<f:selectItem itemValue="#{null}" itemDisabled="#{true}" noSelectOption="#{true}" itemLabel="choose one" />
  <f:passThroughAttributes value="#{bean.attributes}" />
</f:selectItem>

public Map<String, String> getAttributes() {
    Map<String, String> attributes = new HashMap<>();
        if (value == null || "".equals(value)) {
           attributes.put("selected", "selected");
        }
    }   
    return attributes;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM