验证错误selectOneListbox。实体@OneToOne关系

时间:2014-11-29 15:27:28

标签: jsf jsf-2 primefaces

我尝试制作与以下链接相同的列表: http://www.primefaces.org/showcase/ui/input/listbox.xhtml

不幸的是我得到验证错误:

Select Box: Validation Error: Value is not valid

我不知道我在哪里弄错了。我读了很多关于这个问题(在大多数情况下它是由BalusC解决的)但是仍然无法找到问题。

如果你能提供帮助我真的很感激。

StoreHouse.java

@Entity
public class StoreHouse implements Serializable {

@Id
@GeneratedValue
private Integer id;

@OneToOne
private Supply supply;

@Column(nullable = false)
private Integer amount;
//geters setters namedqueryies

Supply.java

@Entity
public class Supply implements Serializable {

@Id
@GeneratedValue
private Integer id;

@Column(unique = true, nullable = false, length = 32)
private String name;

@Column(length = 1024)
private String description;

@Column
private Double price;

@Enumerated(EnumType.STRING)
@NotNull
private SupplyType supplyType;
//geters setters namedqueryies

StoreHouseController.java

@ManagedBean
public class StoreHouseController implements Serializable {

@Inject
private StoreHouseBean storeHouseBean; // DAO for storeHouse 

@ManagedProperty("#{supplyController}")
private SupplyController supplyController; //Manged bean for Supply 

private StoreHouse storeHouse = new StoreHouse();
private List<Supply> allSupplies;

@PostConstruct
public void init() {
    allSupplies = supplyController.findAll();
}

public void check() {
    System.out.println("storeHousetheme" + storeHouse.toString()); // Function just to check if the supply was set
}

SupplyConverter.java

@FacesConverter("supplyConverter")
public class SupplyConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {

    if (value != null && value.trim().length() > 0) {

        try {
            SupplyController supplyController = (SupplyController) context.getExternalContext().getApplicationMap().get("supplyController");
            Supply supply = supplyController.findById(Integer.parseInt(value));
            System.out.println("CONVERTER:" + supply.toString());
            return supply;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion error", "ERROR."));
        }
    } else {
        return null;
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
    if (object != null) {
        return String.valueOf(((Supply) object).getId());
    } else {
        return null;
    }
}
}

view.xhtml

<h:form>
<p:messages autoUpdate="true" />
<h:panelGrid columns="2">
    <h:outputLabel value="Nazwa" />
    <p:selectOneListbox id="supplies" value="#{storeHouseController.storeHouse.supply}" converter="supplyConverter" var="s" filter="true" label="Select Box">
        <f:selectItems value="#{storeHouseController.allSupplies}" var="supply" itemLabel="#{supply.name}" itemValue="#{supply}"/>
        <p:column>
            <h:outputText value="#{s.name}" />
        </p:column>
    </p:selectOneListbox>
    <p:commandButton action="#{storeHouseController.check}" type="submit" value="submit" />
</h:panelGrid>

如果您需要任何其他文件/类,请添加评论。

2 个答案:

答案 0 :(得分:0)

您的SelectItem值是您的对象,因此调用toString()方法并获得类似“Supply @ 44de6bae”的内容。 然后,您的控制器尝试将其解析为int并失败。尝试...

<f:selectItems value="#{...}" var="..." itemLabel="..." itemValue="#{supply.id}"/>

如果在加载页面时发生错误,这可能会有所帮助,并且至少某些组件不喜欢“none-basic-type-or-enum”值。我希望这有帮助,Greez。

答案 1 :(得分:0)

经过几天的搜索和谷歌搜索,我找到了解决方案。我有点生气,因为我已经失去了很多时间来解决这个问题。

结果是我没有覆盖equalsSupply类的方法。

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final Supply other = (Supply) obj;
    if (!Objects.equals(this.id, other.id)) {
        return false;
    }
    if (!Objects.equals(this.name, other.name)) {
        return false;
    }
    if (this.supplyType != other.supplyType) {
        return false;
    }
    return true;
}

我相信这个话题将有助于未来的人......