带有POJO和转换器问题的Facelet selectOneMenu [英] Facelet selectOneMenu with POJOs and converter problem

查看:203
本文介绍了带有POJO和转换器问题的Facelet selectOneMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带公式器和下拉菜单的小页面.用户可以使用下拉菜单选择Lieferant类型的POJO:

I want to have a facelet page with a formular and a dropdown menu. With the dropdown menu the user shoul select a POJO of the type Lieferant:

public class Lieferant extends AbstractPersistentWarenwirtschaftsObject {

    private String firma;

    public Lieferant(WarenwirtschaftDatabaseLayer database, String firma) {
        this(database, null, firma);
    }

    public Lieferant(WarenwirtschaftDatabaseLayer database, Long primaryKey, String firma) {
        super(database, primaryKey);
        this.firma = firma;
    }

    public String getFirma() {
        return firma;
    }

    @Override
    public String toString() {
        return getFirma();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((firma == null) ? 0 : firma.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Lieferant other = (Lieferant) obj;
        if (firma == null) {
            if (other.firma != null)
                return false;
        } else if (!firma.equals(other.firma))
            return false;
        return true;
    }

}

这是我编写的facelet代码:

Here is the facelet code that I wrote:

<h:selectOneMenu> 

标签.该标签应显示Lieferant类型的POJO(不是bean)的列表.这是facelet代码:

tag. This tag should display a list of POJOs (not beans) of the type Lieferant. Here is the facelet code:

<h:selectOneMenu id="lieferant" value="#{lieferantenBestellungBackingBean.lieferant}">
 <f:selectItems var="lieferant" value="#{lieferantenBackingBean.lieferanten}" itemLabel="#{lieferant.firma}" itemValue="#{lieferant.primaryKey}" />
 <f:converter converterId="LieferantConverter" />
</h:selectOneMenu>

这是经过防御的托管后备豆

Here is the refenrenced managed backing bean

@ManagedBean
@RequestScoped
public class LieferantenBackingBean extends AbstractWarenwirtschaftsBackingBean {

    private List<Lieferant> lieferanten;

    public List<Lieferant> getLieferanten() {
        if (lieferanten == null) {
            lieferanten = getApplication().getLieferanten();
        }
        return lieferanten;
    }

}

据我所知,我需要一个自定义转换器,以便在POJO和Lieferant对象的String表示形式之间交换信息.转换器的外观如下:

As far as I know, I need a custom converter, to swap beetween POJO and String representations of the Lieferant objects. Here is what the converter looks like:

@FacesConverter(value="LieferantConverter")
public class LieferantConverter implements Converter {

 @Override
 public Object getAsObject(FacesContext context, UIComponent component, String value) {
  long primaryKey = Long.parseLong(value);
  WarenwirtschaftApplicationLayer application = WarenwirtschaftApplication.getInstance();
  Lieferant lieferant = application.getLieferant(primaryKey);
  return lieferant;
 }

 @Override
 public String getAsString(FacesContext context, UIComponent component, Object value) {
  return value.toString();
 }

}

页面加载没有任何错误.当我填写公式并提交时,页面上会显示一条错误消息:

The page loads without any error. When I fill out the formular and submit it, there is an error message displayed on the page:

Bestellung:lieferantenBestellungForm:lieferant: Validierungsfehler: Wert ist keine gültige Auswahl
translated: validation error: value is not a valid selection

不幸的是,它没有说出它在谈论哪个值.转换器似乎正常工作.

Unfortunaltely it does not say which value it is talking about. The converter seems to work correctly.

我发现了这个与stackoverflow类似的问题和这个 有关selectOneMenu和转换器的文章,但我无法在我的代码中找到问题.为什么

I found this similar question from stackoverflow and this article about selectOneMenu and converters, but I was not able to find the problem in my code. Why is

List<SelectItem> 

在第二个链接的示例中使用

.给我同样的错误.

used in the example from the second link. Gives the same error for me.

任何帮助将不胜感激.预先感谢.

Any help would be appreciated. Thanks in advance.

推荐答案

验证错误:值不是有效的选择

validation error: value is not a valid selection

此错误意味着所选项目与列表中的任何所选项目都不匹配.在您的情况下,这可能有两个原因:Object#equals()被错误地实现,或者<f:selectItems>后面的getter在后续请求(提交表单期间)中未返回与初始请求(之前)相同的列表.在选择值时提交表单.

This error means that the selected item does not match any of the select items in the list. In your case this can have two causes: either Object#equals() is wrongly implemented, or the getter behind <f:selectItems> doesn't return the same list in the subsequent request (during submitting the form) as it did during the initial request (before submitting the form, during selecting the value).

要排除一个或另一个:您可以使用普通香草"测试用例轻松测试Object#equals(),并且可以通过将bean放入会话范围来测试列表的一致性.

To exclude the one or other: you can easily test Object#equals() with a "plain vanilla" testcase and you can test the consistency of the list by putting the bean in session scope.

需要明确的是,您的转换器看起来不错并且工作正常(否则您将无法得到这种错误消息;因此转换步骤已成功完成).

To be clear, your converter looks fine and seems to work fine (else you wouldn't be able to get this kind of error message; the conversion step was thus completed successfully).

这篇关于带有POJO和转换器问题的Facelet selectOneMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆