简体   繁体   English

使用SelectOneMenu在JSF(PrimeFaces)中转换:验证错误

[英]Converter in JSF (PrimeFaces) with SelectOneMenu: Validation Error

I'm making a project in JSF with PrimeFaces and I have a problem with <p:SelectOneMenu> , specifically with a conversion of selected value from an object to String. 我正在使用PrimeFaces在JSF中创建一个项目,我遇到了<p:SelectOneMenu> ,特别是将所选值从对象转换为String。 I have written required Converter class, implemented toString() and equals() methods, as I think, quite correct. 我已经编写了必需的Converter类,实现了toString()equals()方法,我认为非常正确。 However, in a <h:messages /> component it endlessly gives me: 但是,在<h:messages />组件中它无休止地给了我:

j_idt7:j_idt92: Validation Error: Value is not valid
j_idt7:j_idt98: Validation Error: Value is not valid

I have to choose values which are part of the one bigger form. 我必须选择属于一个较大形式的值。 Then, based on the selected owner (właściciel) and company (firma), I add them to the database. 然后,根据所选的所有者(właściciel)和公司(firma),我将它们添加到数据库中。

This is my <p:SelectOneMenu> (twice - two menus): 这是我的<p:SelectOneMenu> (两次 - 两个菜单):

<p:selectOneMenu value="#{wniosek.selectedWl}" var="w">
<f:selectItem itemLabel="Wybierz" itemValue=""/>
   <f:selectItems value="#{wniosek.listaWl}" var="wlasciciel" 
      itemLabel="#{wlasciciel.nazwisko}" itemValue="#{wlasciciel}" />

      <p:column>
          #{w.nazwisko}
      </p:column>
      <f:converter converterId="WlascicielConverter" />
</p:selectOneMenu>                      
 <h:outputText value="Nazwa firmy: "/>

 <p:selectOneMenu value="#{wniosek.selectedFi}"  var="f">
      <f:selectItem itemLabel="Wybierz" itemValue=""/>
      <f:selectItems value="#{wniosek.listaFi}" var="firma" 
           itemLabel="#{firma.nazwa}" itemValue="#{firma}" />
      <f:converter converterId="FirmaConverter" />
      <p:column>
           #{f.nazwa}
      </p:column>
 </p:selectOneMenu>

This is my Converter class for an owner's <p:SelectOneMenu> (similarly I've done with the company one): 这是我所有者的<p:SelectOneMenu>转换器类(类似我已完成公司的一个):

public class WlascicielConverter implements Converter {

int i = 0;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
    try {           
        if (arg2 == null || arg2.isEmpty()) {
            return null;
        }
        String owner = arg2;
        return WlascicielBean.findAnOwner(owner);

    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

    if(arg2 == null) return null;

    Wlasciciel owner = new Wlasciciel();

    if(arg2 instanceof Wlasciciel) {
        owner = (Wlasciciel)arg2;
        System.out.println(owner.getNazwisko());
        String surname = owner.getNazwisko();
        return (surname != null) ? String.valueOf(surname) : null;
    } else throw new ConverterException("Something wrong!" + arg2.hashCode() + arg2.toString());

}

} }

An equals() method: 一个equals()方法:

@Override
public boolean equals(Object obj) {
    if (obj == this) return true;
    if (!(obj instanceof Wlasciciel)) return false;

    Wlasciciel wl = (Wlasciciel)obj;
    if (this.id_w != wl.getId_w()) return false; 
    if (!this.nazwisko.equals(wl.getNazwisko())) return false; 
    if (!this.imie.equals(wl.getImie())) return false;      
    if (!this.ulica.equals(wl.getUlica())) return false; 
    if (this.nr != wl.getNr()) return false; 
    if (this.lokal != wl.getLokal()) return false; 
    if (this.id_n != wl.getId_n()) return false; 

    return true;
}

Could you give me some hints about solving this problem? 你能给我一些解决这个问题的提示吗? I've read many tutorials about the converters in JSF, tried many ways to improve it, but it still doesn't work. 我已经阅读了很多关于JSF中的转换器的教程,尝试了很多方法来改进它,但它仍然不起作用。 I don't know... maybe the problem is somewhere else in my code? 我不知道......问题可能在我的代码中的其他地方? Because of its length I wouldn't paste it here (of course, if it's necessary, I'll do it). 由于它的长度我不会在这里粘贴(当然,如果有必要,我会这样做)。

In total I think you are on the right way, even though the non-english code makes it really difficult to understand what you are actually doing there... 总的来说,我认为你是在正确的方式,即使非英语代码使你真的很难理解你在那里做了什么......

What happens is: You have a Collection of something. 会发生什么:你收集了一些东西。 Your Converter runs through that collection and calls getAsString() on each object. 您的Converter运行该集合并在每个对象上调用getAsString() After you selected something in the browser, the value (return value of getAsString() ) will be transmit to the converter and getAsObject() will be called. 在浏览器中选择了某些内容后, valuegetAsString()返回值)将传输到转换器并调用getAsObject() This is a completely NEW object and not necesssarily one of the collection from before (unless your Converter will actually access that collection and take it from there). 这是一个完全新的对象,并不一定是之前的集合之一(除非您的Converter实际上访问该集合并从那里获取)。 After that JSF will take the collection it used to generate the selectOneMenu and compare every item with the one that the converter returned. 之后,JSF将采用它用于生成selectOneMenu的集合,并将每个项目与转换器返回的项目进行比较。

This might either happen by equals() or hashCode() , depending on what collection was used. 这可能由equals()hashCode() ,具体取决于使用的集合。 So you need to override both at the same time (which you actually ALWAYS should do). 所以你需要同时覆盖它们(你实际上应该总是这样做)。

If JSF couldn't find any object from the collection that matches the one that the converter returned (equals is true, or the hashCode is the same) then you get Validation Error: Value is not valid . 如果JSF找不到集合中与转换器返回的对象相匹配的任何对象(equals为true,或者hashCode相同),则会得到Validation Error: Value is not valid

I hope that cleared the process up a bit. 我希望这个过程有点清楚。 My guess is that you either have to implement hashCode() , or that your equals() method is wrong. 我猜你要么必须实现hashCode() ,要么你的equals()方法是错误的。 The setup in total is correct. 总体设置是正确的。 You need to debug and check why exactly JSF fails to find the object in your list. 您需要调试并检查为什么JSF无法在列表中找到对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM