JSF - h:从不调用selectOneMenu Setter

时间:2017-10-09 08:42:45

标签: java jsf

无法理解<h:selectOneMenu>组件。试过很多方法将参数从下拉列表传递给java,以及4种不同的方法来调用java方法。前两种方式(onchange和commandbutton不会调用java)其他两种方式,但由于无法将selectedItem设置为java变量,因此效果很差:

<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>

和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);
}

在控制台中返回null:

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

但是我可以看到outputText的值总是&#34; All&#34;并且在控制台中我每次调用getSelectedItem方法时都会看到:

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

此外,我认为这非常重要:在我选择任何值后,页面刷新并且它将成为默认状态&#39;全部&#39;价值再次。

2 个答案:

答案 0 :(得分:0)

你真的想发送整个表格(做表格提交)吗?

试试这个,应该没有页面刷新:

<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>

答案 1 :(得分:0)

@ujulu是对的,我用@Scope(ScopeType.SESSION)创建了单独的java类,我的表单位于错误的位置,我在层次结构中将其移动得更高,以摆脱任何父组件。它现在有效。