简体   繁体   中英

JSF - h:selectOneMenu Setter is never called

Cant understand <h:selectOneMenu> component. Tried many ways to pass parameter from dropdown to java, and 4 different ways to call java method. First two ways (onchange and commandbutton doesnt call java) other two does, but works badly as it cant set selectedItem to java variable:

<h:form id="tasks">

    <h:selectOneMenu id = "selectonemenu" value="#{services.selectedItem}"
    immediate="true" onchange="javascript: return this.form.submit();">
        <f:selectItems value="#{services.selectItems}" />
    </h:selectOneMenu>

    <h:commandButton value = "StringHello" action="#{services.StringHelo}" />

    <s:link action="#{services.StringHello}" reRender="tasks"></s:link>

    <a:commandLink id="aclink"
                                action="#{services.StringHello}"
                                ajaxSingle="true" eventsQueue="globalQueue"
                                ignoreDupResponses="true" requestDelay="0"
                                onComplete="return false;"
                                status="globalStatus">
                                aclink
                                </a:commandLink>
    <h:outputText id = "valueofselected" value = "#{services.selectedItem}"/>
</h:form>

And Services.Java:

private String selectedItem;

public String getSelectedItem() {
    if (selectedItem == null) {
        selectedItem = "All"; // This will be the default selected item.
    }
    System.out.println("getSelectedItem "+selectedItem);
    return selectedItem;
}

public void setSelectedItem(String selectedItem) {
    this.selectedItem = selectedItem;
    System.out.println("setSelectedItem");
}

public List getSelectItems() {
    List selectItems = new ArrayList();
    selectItems.add(new SelectItem("All", "All"));
    selectItems.add(new SelectItem("A", "A"));
    selectItems.add(new SelectItem("B", "B"));
    selectItems.add(new SelectItem("C", "C"));
    selectItems.add(new SelectItem("D", "D"));
  System.out.println("getSelectItems: "+selectItems.size());
    return selectItems;
}
public void StringHello(){
    System.out.println(" SelectedItem - "+selectedItem);
}

returns null in console:

14:41:51,897 INFO  [STDOUT]  SelectedItem - null

but i can see that outputText's value is always "All" and in console i see every time when getSelectedItem method is called:

14:41:49,087 INFO [STDOUT] getSelectedItem All

Also, i think it's very important: after i choose any value, page refreshes and it becomes default 'All' value again.

Do you really want to send the whole form (do a form submit)?

Try this, there should be no page refresh:

<h:form id="tasks">

    <h:selectOneMenu value="#{services.selectedItem}">
        <f:selectItems value="#{services.selectItems}" />
        <f:ajax execute="@this" render="result"/>
    </h:selectOneMenu>

    <h:outputText id="result" value="#{services.selectedItem}"/>
</h:form>

@ujulu was right, i created separate java class with @Scope(ScopeType.SESSION) and my form was located at wrong place, i moved it higher in hierarchy, to get rid of any parent components. And it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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