You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将PrimeFaces SelectOneMenu手动选项值传入表格列?

问题解决:PrimeFaces selectOneMenu无法传递选中值到BeanController

问题场景

我有一个单表小型项目,需要通过PrimeFaces的selectOneMenu为表格列赋值,该组件的选项为手动定义,而非预加载列表。最初遇到的问题是:选中值无法传递到BeanController,尝试绑定#{vot.lista}(对应表格列)但没有效果。

问题原因

核心问题是未在Controller Bean中保存当前处理的记录,导致组件绑定的对象无法被正确识别和更新。

解决方案

通过在Controller中添加read方法保存当前选中的记录,再调用modify方法执行更新操作,即可实现功能。

初始页面代码(存在问题)

<p:dialog id="dlgListas" widgetVar="dialogoListas" modal="true" closable="false" closeOnEscape="true" resizable="false"
          draggable="true" visible="#{facesContext.validationFailed}">
    <p:ajax event="close" update="dlgListas"/>
    <div class="field col-12 md:col-4">
        <p:outputLabel for="@next" value="Registrar Lista"/>
        <p:selectOneMenu id="opcionLista" value="#{vot.lista}">
            <f:selectItem itemLabel="Seleccionar Lista" itemValue=""/>
            <f:selectItem itemLabel="Lista 1" itemValue="1"/>
            <f:selectItem itemLabel="Lista 2" itemValue="2"/>
            <f:selectItem itemLabel="Lista 3" itemValue="3"/>
        </p:selectOneMenu>
        <p:commandButton value="Confirmar" immediate="true" onclick="PF('dialogoListas').hide();"/>
    </div>
</p:dialog>

修正后的Bean代码

public void read(Delegado delegadoSeleccionado){
    votante = delegadoSeleccionado;
}
   
public void modify(){
    votanteEJB.edit(votante);
}

修正后的页面代码

<p:dialog id="dlgListas" widgetVar="dialogoListas" modal="true" closable="true" closeOnEscape="true" resizable="false"
          draggable="true" visible="#{facesContext.validationFailed}" header="Registrar Lista">
    <!-- <p:ajax event="close" update="dlgListas"/> -->
    <!--<div class="field col-12 md:col-4"> -->
    <h:panelGrid columns="1">
        <p:outputLabel for="opcionLista" value="Registrar Lista"/>
        <p:selectOneMenu id="opcionLista" value="#{delegadoController.votante.lista}">
            <f:selectItem itemLabel="Seleccionar Lista" itemValue=""/>
            <f:selectItem itemValue="1" itemLabel="Lista 1"/>
            <f:selectItem itemValue="2" itemLabel="Lista 2"/>
            <f:selectItem itemValue="3" itemLabel="Lista 3"/>
        </p:selectOneMenu>
        <p:commandButton value="Confirmar" actionListener="#{delegadoController.modify()}" onclick="PF('dialogoListas').hide();"/>
    </h:panelGrid>
    <!--</div>-->
</p:dialog>

内容的提问来源于stack exchange,提问作者Gustavo Echenique

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 21:15:47