首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >SelectOneMenu始终显示第一个项目,而不是选定的项目- Primefaces

SelectOneMenu始终显示第一个项目,而不是选定的项目- Primefaces
EN

Stack Overflow用户
提问于 2016-10-09 03:09:11
回答 2查看 1.8K关注 0票数 0

在这个问题上,我们将不胜感激。

我有几个JSF页面,但其中一个是子类别,其中有一个用于选择类别SelectOneMenu,但是当我尝试编辑子类别时,这个SelectOneMenu总是显示第一个值,并且没有预先选择。

我怎么才能解决这个问题。我已经读了很多关于的帖子,,甚至尽管我已经实现了一些建议,但我并没有做到。例如,一个@BalusC

primefaces selectOneMenu doesn't working when it should

这里的视图是

代码语言:javascript
复制
<h:outputText value="Subcategory ID : #{subcategoryController.subcategory.subcategoryId}"/>

<p:selectOneMenu id="cboCategoryDialog" converter="subcategoryConverter"
                 value="#{subcategoryController.category.categoryId}"
                 style="width: 100%"

    <f:selectItems value="#{subcategoryController.categoryList}"
                   var="subcat"
                   itemLabel="#{subcat.categoryName}"
                   itemValue="#{subcat.categoryId}"/>
</p:selectOneMenu>**

这是子类别实体:

代码语言:javascript
复制
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "subcategory_id")
private Integer subcategoryId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "subcategory_name")
private String subcategoryName;
@JoinColumn(name = "category_id", referencedColumnName = "category_id")
@OneToOne(optional = false;
private Category categoryId;

//Getters and Setters Methods

@Override
public int hashCode() {
    return (subcategoryId != null)
            ? (this.getClass().hashCode() + subcategoryId.hashCode())
            : super.hashCode();
}

@Override
public boolean equals(Object obj) {

    return (obj instanceof Subcategory) && (subcategoryId != null)
            ? subcategoryId.equals(((Subcategory) obj).subcategoryId)
            : (obj == this);
}

SubcategoryConverter

代码语言:javascript
复制
@RequestScoped
@FacesConverter("subcategoryConverter")
public class SubcategoryConverter implements Converter {

@EJB
private SubcategoryFacadeLocal EJBsubcategory;

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (!(value instanceof Subcategory) || ((Subcategory) value).getSubcategoryId() == null) {
        return null;
    }

    return String.valueOf(((Subcategory) value).getSubcategoryId());
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
    if (value == null || !value.matches("\\d+")) {
        return null;
    }

    Subcategory subcategory = EJBsubcategory.find(Integer.valueOf(value));

    if (subcategory == null) {
        throw new ConverterException(new FacesMessage("Unknown operation ID: " + value));
    }

    return subcategory;

    }
}

下图显示了当我尝试编辑任何项目时的对话框,它总是显示computer,因为它是第一个itm。

See the image showing the issue

我一直在阅读一些与此相关的有用问题,但我无法解决它。

提前谢谢你,

EN

回答 2

Stack Overflow用户

发布于 2016-11-01 02:03:48

在检查代码之后,我意识到我在JSF.中使用了错误的实体

为例:

在问题中,我发布了: subcategoryController.category.categoryId将类别对象保存在子类别对象中。

如下所示:

代码语言:javascript
复制
<p:selectOneMenu id="cboCategoryDialog" converter="subcategoryConverter"
    value="#{subcategoryController.category.categoryId}"

但是,正确的方法如下:

代码语言:javascript
复制
<p:selectOneMenu id="cboCategoryDialog" converter="omnifaces.SelectItemsConverter"
    value="#{subcategoryController.subcategory.categoryId}"

现在,这个组件“指向”了正确的对象。

我面临的另一个问题是:

因为我有嵌套的实体,例如:Entity.entity.entity,在使用它们之前,它们应该在每个级别上初始化。

在这一点上,一些帖子真的、真的、真的对我有帮助:

关于解决嵌套实体问题的(PropertyNotFoundException).

Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable

在SelectOneMenu上使用normal和OmniFaces转换器:

http://balusc.omnifaces.org/2007/09/objects-in-hselectonemenu.html

票数 1
EN

Stack Overflow用户

发布于 2018-06-05 06:38:21

确保实现了实体hashCode()和equals()方法,并且不尝试访问可以为空的字段。这对我很有效!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39936306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档