使用自定义转换器时JSF验证错误 [英] JSF Validation Error While Using Custom Converter
问题描述
我正在使用JSF设置一个表单(我对此很新)我得到一个验证错误:值无效
消息中的一个领域。这个字段实际上是一个单独的对象(我将在下面显示),它有一个自定义转换器。
I am setting up a form using JSF (I'm pretty new at this) and I am getting a Validation Error: Value is not valid
message on one of the fields. This field is actually a separate object (as I will show below) that has a custom converter.
这是我所拥有的(删除了不相关的代码):
Here is what I have (with non-relevant code removed):
我有一个 Citation
类:
@ManagedBean(name="citation")
public class Citation {
private int id;
private Status status;
// getters and setters
}
我也有您在引用
类中看到的状态
类:
I also have a Status
class that you see referenced in the Citation
class:
@ManagedBean(name="status")
public class Status {
private int id;
private String name;
// getters and setters
public List<Status> getAllStatuses() {
Session session = HibernateUtil.getCurrentSession();
session.beginTransaction();
session.clear();
Query query = session.createQuery("from Status");
List<Status> statuses = query.list();
try {
session.getTransaction().commit();
} catch (HibernateException e) {
// TODO: handle exception
session.getTransaction().rollback();
}
return statuses;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (!(obj instanceof Status)) return false;
if (this.id == ((Status)obj).getId()) {
return true;
} else {
return false;
}
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
然后对于我的表格,我有:
Then for my form, I have:
<h:selectOneMenu id="citation_status" value="#{citation.status}">
<f:selectItems value="#{status.allStatuses} var="s" itemValue="#{s.id}" itemLabel="#{s.name}" />
</h:selectOneMenu>
<h:message for="citation_status" />
最后,对于我的转换器,我有:
Lastly, for my converter, I have:
@FacesConverter(forClass=Status.class)
public class StatusConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// uses Hibernate to get the Status object (using a breakpoint in Eclipse, I have verified that this works)
// I can post this code if needed, but just trying to keep it short :)
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return String.valueOf(((Status) value).getId());
}
}
现在,当我到达我的表单并提交时,我得到状态旁边的验证错误
。我很擅长这一点,感谢@BalusC,我就是这么远。
Now when I get to my form and submit, I get the Validation Error
next to the Status. I'm pretty new at this and thanks to @BalusC, I'm this far along.
非常感谢任何帮助。
推荐答案
验证错误:值无效
Validation Error: Value is not valid
如果< h:selectOneMenu>
,只要所选项目与列表中的任何可用项目不匹配,您就会收到错误消息。即 selectedItem.equals(selectItem)
从未为任何项目返回 true
。
In case of <h:selectOneMenu>
, you will get this when error whenever the selected item does not match any of the items available in the list. I.e. selectedItem.equals(selectItem)
has never returned true
for any of the items.
因为它显然是一个自定义对象( Status
类),你实现了它的 Object#equals()
(和 #hashCode()
)正确吗?如有必要,您可以让IDE(Eclipse / Netbeans)自动生成它们。
Since it's apparently a custom object (the Status
class), did you implement its Object#equals()
(and #hashCode()
) properly? You can if necessary let the IDE (Eclipse/Netbeans) autogenerate them.
- Overriding equals and hashCode in Java
- How to implement equals() in beans/entities
更新:拥有仔细看看你的代码,事实证明你实际上是在提交#{s.id}
而不是#{s}
(整个状态
对象)。相应地修复 itemValue
它应该有效(如果 equals()
仍在正常工作)。
Update: after having a closer look at your code, it turns out that you're actually submitting #{s.id}
instead of #{s}
(the whole Status
object). Fix the itemValue
accordingly and it should work (if equals()
is still doing its job properly).
这篇关于使用自定义转换器时JSF验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!