java selectonemenu 验证错误:值无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16050051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selectonemenu Validation Error: Value is not valid
提问by user2270884
I'm using selectonemenu like this:
我正在使用 selectonemenu 这样的:
<h:selectOneMenu value="#{MyBean.zajecie.przedmiot}">
<f:selectItems value="#{MyBean.przedmioty}" var="p"
itemLabel="#{p.nazwa}" itemValue="#{p}" />
<f:converter converterId="converter.PrzedmiotConverter" />
</h:selectOneMenu>
MyBean:
我的豆:
private Zajecie zajecie;//+set get
private List<Przedmiot> przedmioty;//+set get
@PostConstruct
private void init() {
przedmioty = przedmiotDao.findByLogin("login");
zajecie = new Zajecie();
}
and the converter methods:
和转换器方法:
public Object getAsObject(FacesContext context, UIComponent component, String value) {
PrzedmiotDao przedmiotDao = DaoFactory.getInstance().getPrzedmiotDao();
Przedmiot przedmiot = przedmiotDao.findById(Przedmiot.class, Integer.parseInt(value));
return przedmiot;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
Przedmiot przedmiot = (Przedmiot) value;
String idAsString = String.valueOf(przedmiot.getPrzedmiotId());
return idAsString;
}
The selectonemenu component is being populated as it's supposed to. When I submit, it shows Validation Error: Value is not valid
. I know i need a proper equals()
method for my entities so I've generated it with eclipse using only the id field. Then i had to change the test getClass() != obj.getClass()
to obj instanceof Przedmiot
because obj.getClass()
returned something like this: Przedmiot_$$_javassist_1
. I'm not sure if that is relevant because after all obj
proves to be null
. What am I doing wrong?
selectonemenu 组件正在按预期填充。当我提交时,它显示Validation Error: Value is not valid
. 我知道equals()
我的实体需要一个合适的方法,所以我只使用 id 字段用 eclipse 生成了它。然后我不得不测试更改getClass() != obj.getClass()
到obj instanceof Przedmiot
因为obj.getClass()
返回类似这样的:Przedmiot_$$_javassist_1
。我不确定这是否相关,因为毕竟obj
证明是null
. 我究竟做错了什么?
Edit:
编辑:
MyBean is ViewScoped.
MyBean 是 ViewScoped。
Funny thing is that similar code using the same converter works in an other part of the application. The difference is that in the working part I'm just viewing the list of type Przedmiot
and I'm obtaining it in another way.
有趣的是,使用相同转换器的类似代码可以在应用程序的其他部分运行。不同之处在于,在工作部分,我只是查看类型列表,Przedmiot
而我正在以另一种方式获取它。
@PostConstruct
private void init() {
student = studentDao.findByLogin(ra.getUser());
}
<h:selectOneMenu value="#{otherBean.przedmiot}">
<f:selectItems value="#{otherBean.student.grupa.przedmiots}" var="p"
itemLabel="#{p.nazwa}" itemValue="#{p}" />
<f:converter converterId="converter.PrzedmiotConverter" />
</h:selectOneMenu>
回答by user2270884
Solved it. It was of course badly written equals()
method.
First of all there was a mistake in my question. obj
didn't resolve to null but other.przedmiotId
did. Sorry for that. Look at the method generated by eclipse:
解决了。这当然是写得很糟糕的equals()
方法。首先,我的问题有一个错误。obj
没有解决为空,但other.przedmiotId
做到了。对不起。看eclipse生成的方法:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Przedmiot))//changed this from (getClass() != obj.getClass())
return false;
Przedmiot other = (Przedmiot) obj;
if (przedmiotId == null) {
if (other.przedmiotId != null)
return false;
} else if (!przedmiotId.equals(other.przedmiotId))
return false;
return true;
}
The issue is in other.przedmiotId
. When obtaining the value with a getter other.getPrzedmiotId()
it doesn't resolve to null anymore.
问题在other.przedmiotId
. 使用 getter 获取值时,other.getPrzedmiotId()
它不再解析为 null。
回答by Rong Nguyen
In your converter: Integer.parseInt(value)
, and in <f:selectItems
you set itemValue="#{p}"
, so each #{p}
is instance of Przedmiot
type.
在您的 converter:Integer.parseInt(value)
和<f:selectItems
您的 set 中itemValue="#{p}"
,因此每个#{p}
都是Przedmiot
类型的实例。
See also: Why selectOneMenu Send ItemLabel to the converter?