使用自定义转换器时JSF验证错误

编程入门 行业动态 更新时间:2024-10-27 10:24:36
本文介绍了使用自定义转换器时JSF验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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()mit(); } 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.

  • 用Java覆盖equals和hashCode
  • 如何在bean /实体中实现equals()
  • 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验证错误

本文发布于:2023-11-05 21:24:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   转换器   错误   JSF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!