how to get value from ?

前端 未结 1 1810
陌清茗
陌清茗 2021-01-25 00:46

i want to get the value from . i tryed this code but it doesn\'t work :


        
        

        
相关标签:
1条回答
  • 2021-01-25 01:46

    You've after all 2 problems.

    First, the error "erreur de validation. La valeur est incorrecte" which is the French translation of "Validation Error: Value is not valid" means that the submitted value doesn't equals() any one of the available items in <f:selectItems>. Your code is not complete enough to point out the root cause, but I guess that you've a List<Department> there in <f:selectItems value> and thus every item is Department, but you're trying to set it as a String value of id instead of as Department. This is not right. You need to supply a converter between Department and String and use #{departementController.selected} instead.

    Something like this:

    <h:selectOneMenu value="#{bean.selectedDepartment}">
        <f:selectItems value="#{bean.availableDepartments}" />
    </h:selectOneMenu>
    

    with

    private Department selectedDepartment;
    private List<Department> availableDepartments;
    

    And a @FacesConverter which converts between Department and its unique String representation.

    Your second problem is that you seem to be focusing too much on JSF 1.x targeted examples as to populating another field on change of the dropdown. You're using a rather clumsy/hacky JSF 1.x workaround for that. In JSF 2.x you can just use <f:ajax> for this.

    <h:selectOneMenu value="#{bean.selectedDepartment}">
        <f:selectItems value="#{bean.availableDepartments}" />
        <f:ajax listener="#{bean.changeDepartment}" render="inputId" />
    </h:selectOneMenu>
    <h:inputText id="inputId" value="#{bean.input}" />
    

    with

    public void changeDepartment() {
        input = selectedDepartment.getId();
    }
    

    See also:

    • Our selectonemenu wiki page
    0 讨论(0)
提交回复
热议问题