选择<f:selectitem>后,将相关值设置为null

时间:2015-11-20 05:21:51

标签: java jsf

下面的

是我的jsf代码,

<h:outputText value="SP Id" styleClass="required"/>
                    <h:selectOneMenu style="padding-left:60px;" class="input" id="spid" required="true" requiredMessage="Select SP Id"
                        value="#{applicationController.spid}">
                        <p:ajax listener="#{applicationController.onFromChange()}"
                            update="fromnames" />
                        <f:selectItem itemValue="" itemLabel="--Select--" />
                        <f:selectItems value="#{applicationController.spids}"></f:selectItems>
                    </h:selectOneMenu>
                    <h:message for="spid" class="hmsg" />

                    <h:outputText value="Sp Name" class="left1"/>
                    <h:inputText class="input" id="fromnames"
                        value="#{applicationController.spname}" />
                    <h:message for="fromnames" />

支持bean代码是(方法),

public void onFromChange() {

        if (spid != null && !spid.equals("")) {
            int spId = Integer.parseInt(spid);
            spname = baseService.getSalesPersonById(spId);
        } else {
        }
    }

//setter-getters
public String getSpname() {
        return spname;
    }

    public void setSpname(String spname) {
        this.spname = spname;
    }

public List<Integer> getSpids() {
        return spids;
    }

    public void setSpids(List<Integer> spids) {
        this.spids = spids;
    }

从上面的代码中,每件事情都可以。

问题:如果我选择f:selectItems值,相关值(spname) 正在展示。选择f之后:selectItem spname应设置为null但不设置为null,而不是显示之前的值。

1 个答案:

答案 0 :(得分:0)

更改jsf代码如下,

<h:selectOneMenu style="padding-left:60px;" class="input" id="spid" required="true" requiredMessage="Select SP Id"
                        value="#{applicationController.spid}">
                        <p:ajax listener="#{applicationController.onFromChange()}"
                            update="myForm1" event="change" process="@this"/>
                        <f:selectItem itemValue="0" itemLabel="--Select--" noSelectionOption="false" />
                        <f:selectItems value="#{applicationController.spids}"></f:selectItems>
                    </h:selectOneMenu>
                    <h:message for="spid" class="hmsg" />

并将支持bean方法修改为,

public void onFromChange() {

        if (spid != null && !spid.equals("")) {
            try{
            int spId = Integer.parseInt(spid);
            spname = baseService.getSalesPersonById(spId);
            }
            catch(Exception exception){
                spname = null;
            }
        } else {
            spname = null;
        }
    }