简体   繁体   English

selectOneMenu不能显示正确的值,但是getter会得到它

[英]selectOneMenu does not show the proper value but getter gets it

Given this selectOneMenu 给定这个selectOneMenu

<h:selectOneMenu value="#{fb.aktionTxt}">
    <f:selectItems value="#{myBean.FBAEnum}" />
</h:selectOneMenu>

this bean code 这个bean代码

public FehlerBewertungAktionEnum[] getFBAEnum() {
        return FehlerBewertungAktionEnum.values();
}

this enum 这个枚举

public enum FehlerBewertungAktionEnum {

    NEKO_NEKO("NEK0-NEK0           "),
    CURSOR_PARAMETER("CURSOR-PARAMETER    "),
    CURSOR_LEER("CURSOR-LEER         ");

    private final String str;
    FehlerBewertungAktionEnum(String str) { this.str = str;}
    public String toString(){ return str; }

}

and a database value of aktionTxt 和数据库值aktionTxt

|CURSOR_LEER         | (20 chars)

why does it display NEK0-NEK0 (first element of the enum) instead of CURSOR_LEER in my selectOneMenu, when the getter returns |CURSOR_LEER | 当getter返回|CURSOR_LEER |时,为什么在我的selectOneMenu中显示NEK0-NEK0(枚举的第一个元素)而不是|CURSOR_LEER | ? I debugged the jsf page and found that no <option> has the selected attribute. 我调试了jsf页面,发现没有<option>具有selected属性。

The strange thing also is that I can create a new element with the same jsf code and it will properly save my new object to the DB. 奇怪的是,我可以用相同的jsf代码创建一个新元素,并将新对象正确保存到数据库中。 I am pretty sure there is no way to use trim() here since then I cannot save my fb object. 我很确定没有办法在这里使用trim() ,因为那样我将无法保存fb对象。

NEK0-NEK0 is being selected because it is the first one in the list, and none of the items could be matched by value to #{fb.aktionTxt} . NEK0-NEK0选择NEK0-NEK0是因为它是列表中的第一个,并且所有项目都无法通过值与#{fb.aktionTxt}进行匹配。 You could add one default item which will be selected in this case. 您可以添加一个默认项,在这种情况下将被选中。

<h:selectOneMenu value="#{fb.aktionTxt}">
    <f:selectItem itemLabel="Select one..."/>
    <f:selectItems value="#{myBean.FBAEnum}" />
</h:selectOneMenu>

If #{fb.aktionTxt} resolves to (its type is) FehlerBewertungAktionEnum , it should work. 如果#{fb.aktionTxt}解析为(类型为) FehlerBewertungAktionEnum ,则它将正常工作。 But, if it is String then no value from the list would be equal to it because Enum is compared to String , resulting in the first item being selected. 但是,如果它是String则列表中的任何值都不会等于它,因为EnumString进行了比较,从而导致选择了第一项。 In this case, if #{fb.aktionTxt} is String , you could try this for <f:selectItems> 在这种情况下,如果#{fb.aktionTxt}String ,则可以尝试<f:selectItems>

<f:selectItems value="#{myBean.FBAEnum}" var="myEnum" itemLabel="#{myEnum.toString()}" itemValue="#{myEnum.toString()}"/>

Oh - my - god 哦,我的上帝

The database value is |CURSOR_LEER | 数据库值为|CURSOR_LEER | (20 chars) with an underscore while my enum uses a hyphen . (20个字符) 下划线,而我的枚举使用连字符 How could neither me, my colleague or anybody reading this not see this for so long >.< 我,我的同事或任何阅读此书的人都这么长时间都看不到>。<

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM