java - SelectItemsConverter Omnifaces 预选对象值?

标签 java jsf-2 converters selectonemenu omnifaces

我有一个问题,当我将对象保存到数据库时它工作正常,但是当我想从数据库检索它时不起作用,我使用 Omnifaces 的 selectItemsConverter

我有我的对象“Modelo”,里面还有另外两个对象,分别是“Marca”和“Gama” enter image description here

这些是我的 Java 实体(toString() 用于 Omnifaces):

模型:

private Marca marca;
private Gama gama;
getters and setters...
@Override
public String toString() {
    return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}

马卡报:

getters and setters...
@Override
public String toString() {
    return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}

Gamma :

getters and setters...
@Override
public String toString() {
    return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}

这是我的托管Bean

ModeloBean

@ManagedBean
@ViewScoped
public class ModeloBean {

private Modelo modelo = new Modelo();    
getters and setters ...

//This is for call the DB to retrieve the value, and works fine, but i cant show the preselected value to the xhtml
public void leer(Modelo mo) throws Exception {
    ModeloDAO dao = new ModeloDAO();

    try {
        this.init();           
        this.modelo = dao.leer(mo);            
    } catch (Exception e) {
        throw e;
    } finally {
        dao = null;            
    }
}

这是我的 xhtml 页面 我有一个对话框,我用它来保存和更新对象

<p:dialog id="dlgDatos" widgetVar="wdlgDatos" modal="true" appendToBody="true" header="#{modeloBean.accion}" draggable="false" resizable="false">                                                            
                <h:form>
                    <h:panelGrid columns="2">                            
                        <p:outputLabel value="Marca" />
                        <p:selectOneMenu value="#{modeloBean.modelo.marca}" converter="omnifaces.SelectItemsConverter" filter="true" filterMatchMode="startsWith" required="true">
                            <f:selectItem itemLabel="Seleccione" itemValue="#{null}" noSelectionOption="true" />
                            <f:selectItems value="#{marcaBean.lstMarcasVigentes}" var="marca" itemLabel="#{marca.nombre}" itemValue="#{marca}" />
                        </p:selectOneMenu>

                        <p:outputLabel value="Gama" />                            
                        <p:selectOneMenu value="#{modeloBean.modelo.gama}" converter="omnifaces.SelectItemsConverter" filter="true" filterMatchMode="startsWith" required="true">
                            <f:selectItem itemLabel="Seleccione" itemValue="#{null}" noSelectionOption="true" />
                            <f:selectItems value="#{gamaBean.lstGamasVigentes}" var="gama" itemLabel="#{gama.nombre}" itemValue="#{gama}" />
                        </p:selectOneMenu>

                        <p:outputLabel for="txtNombre" value="Modelo" />
                        <p:column>
                            <p:inputTextarea id="txtNombre" value="#{modeloBean.modelo.nombre}" />
                            <p:watermark for="txtNombre" value="Para registrar varios modelos, sepárelos por comas (,)" />
                        </p:column>
                        
                        <p:outputLabel value="Vigencia" rendered="#{modeloBean.accion eq 'Modificar'}"/>
                        <p:selectBooleanCheckbox value="#{modeloBean.modelo.vigencia}" rendered="#{modeloBean.accion eq 'Modificar'}"/>

                        <p:commandButton value="#{modeloBean.accion}" actionListener="#{modeloBean.operar()}" oncomplete="PF('wdlgDatos').hide(); PF('wdtLista').clearFilters();" update=":frmLista:dtLista, :msj"/>
                        <p:commandButton value="Cancelar" immediate="true" onclick="PF('wdlgDatos').hide();"/>
                    </h:panelGrid>
                </h:form>
            </p:dialog>                

selectOneMenu 可以很好地保存,但更新时只能检索字符串值,而不是组合框的预选值

enter image description here

此对话框仅检索“105”的字符串值,因为它是一个字符串和我的复选框“Vigencia”的 boolean 值,但不是我的组合框值。我哪里错了?

enter image description here

最佳答案

我解决了这个问题,将其添加到我的实体中(hashCode 和 equals)

 @Override
public int hashCode() {
    int hash = 5;
    hash = 83 * hash + this.codigo;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Modelo other = (Modelo) obj;
    if (this.codigo != other.codigo) {
        return false;
    }
    return true;
}

关于java - SelectItemsConverter Omnifaces 预选对象值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463253/

相关文章:

java - 在 ZeroMQ 中使用 .poll() 和 .recv() 方法有什么区别?

java - 如何在单个异常中返回异常列表?

java.lang.NoClassDefFoundError : graphql/execution/instrumentation/SimpleInstrumentation (GraphQL and Spring Boot)

jsf-2 - JSF 复合组件 - 尝试保存状态时的奇怪行为

CSS 字体 : Howto convert multiple TTF files into one file?

swift - 如何快速将字符串转换为 Unicode?

java - 使用 Spring Security 的 SiteMinder 注销

JSF 2.0 + Primefaces 2.x : Bind string to calendar

java - JSF 2.0 动态属性,无需创建新组件

types - 如何创建转换器?