简体   繁体   English

如何在JSF2中正确使用f:selectItem?

[英]How to use f:selectItem properly with JSF2?

guys I need to update another selectOneMenu when the User selects one option of combo_pedido_tipoplm selectOneMenu. 伙计们,当用户选择combo_pedido_tipoplm selectOneMenu的一个选项时,我需要更新另一个selectOneMenu。 If the user selects the "Chose One" option it needs to clear the second one. 如果用户选择“选择一个”选项,则需要清除第二个选项。

I've tried everything but I cant call my actionListener after I've selected the Option "Chose one" (Selecione) from my h:selectOneMenu. 我已经尝试了所有方法,但是从h:selectOneMenu中选择了“选择一个”(Selecione)选项后,无法调用actionListener。

XHTML XHTML

<h:selectOneMenu id="combo_pedido_tipoplm" value="#mBeanManterPedido.tipoPlacaMaeFiltro}" required="true" disabled="#{!mBeanManterPedido.pedidoValido or mBeanManterPedido.clonado}">
    <f:selectItem itemLabel="#{msgTemplate.lblSelecione}" />
    <f:selectItems value="#{mBeanManterPedido.selectItemsTipoPlacaMae}" />
    <p:ajax event="change" listener="#{mBeanManterPedido.tipoPlacaMaeChange}"   process="@this"/>
</h:selectOneMenu>

MBean 的MBean

private TipoPlacaMae tipoPlacaMaeFiltro;

public void popularTipoPlacaMae() {
        this.selectItemsTipoPlacaMae = new ArrayList<SelectItem>();
        for (TipoPlacaMae tipoplaca : TipoPlacaMae.values()) {
            this.selectItemsTipoPlacaMae.add(new SelectItem(tipoplaca, tipoplaca.getNome()));
        }
        UtilsCommon.orderByLabel(selectItemsTipoPlacaMae);
    }

public void tipoPlacaMaeChange(AjaxBehaviorEvent e) {
        // deseleciona a PlacaMae atualmente selecionada.
        pedido.setPlacaMae(null);
        limparDadosPedido();
        popularPlacaMae();
    }

if I select the option "Chose one" I got a validation error because this selectOneMenu is requeried and my setter for tipoPlacaMaeFiltro is not called. 如果我选择选项“选择一个”,则会收到验证错误,因为重新查询了该selectOneMenu,并且未调用我的tipoPlacaMaeFiltro设置程序。

<?xml version='1.0' encoding='UTF-8'?>

{"validationFailed":true} { “validationFailed”:真正}

What can I do about it ? 我该怎么办?

For those that have the same doubt. 对于那些有同样疑问的人。

You need to put the immediate="true" in your ajax request. 您需要在ajax请求中放入“ instant =“ true”。

<h:selectOneMenu id="id"
                    value="#{mbean.value}" required="#{mbean.required}"
                    converter="#{mbean.converter}">
                    <f:selectItem itemLabel="Chose one" />
                    <f:selectItems value="#{mbean.selectItems}" />
                    <f:ajax event="valueChange"  listener="#{mbean.onchange}"
                        render="second_combo"
                        immediate="true">
                    </f:ajax>
                </h:selectOneMenu>

After that in the ManagedBean you need to implements but handle differently the listener 之后,您需要在ManagedBean中实现,但以不同的方式处理侦听器

public void onchange(AjaxBehaviorEvent e) {
    YourClass o = getSelectedValue(e);
}


private YourClass getSelectedValue(AjaxBehaviorEvent e)
{
    if (e != null)
    {   
        UISelectOne select = (UISelectOne) e.getSource();
        if (select.getSubmittedValue() == null ||
                select.getSubmittedValue().toString().isEmpty()) {
            return null;
        }
        else {          
            String id = select
                    .getSubmittedValue().toString();
                return ObjectThatImplementsConverterInterface.getAsObject(FacesContext.getCurrentInstance(), select, id);
        }
    }
    return null;
}

Unfortunally you must handle that way because if you don't do the user can selects "Chose One" and the mbean.value will be OK (null) after that the second selection in the same SelectOneMenu will trigger with the previous selected value (null). 不幸的是,您必须以这种方式处理,因为如果不这样做,用户可以选择“选择一个”,并且mbean.value将为OK(空),之后,同一SelectOneMenu中的第二个选择将以先前选择的值触发(空)。 I don't know if it happens with everybody or just me. 我不知道这是发生在所有人身上还是我身上。

Bye. 再见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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