【问题标题】:set related values to null after selecting <f:selectItem>选择 <f:selectItem> 后将相关值设置为 null
【发布时间】:2015-11-20 05:21:51
【问题描述】:

下面是我的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" />

backing 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,而不是显示以前的值。

【问题讨论】:

    标签: java jsf


    【解决方案1】:

    修改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;
            }
        }
    

    【讨论】:

    • 你能补充一些解释为什么应该改变吗?
    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多