java 下拉菜单“'null Converter'的转换错误设置值''”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10851117/
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
dropdown menu "Conversion Error setting value '' for 'null Converter'"
提问by user1423793
So i am using seam to try to make a dropdown menu to add a delivery to a database. I'm using a drop down menu to select which employee from the database is doing the delivery. The menu loads all the employees in the database just fine but when I select one and click add, I get the error "Conversion Error setting value '(hash for employee)' for 'null Converter'.
所以我正在使用 seam 尝试制作一个下拉菜单以将交付添加到数据库中。我正在使用下拉菜单从数据库中选择哪个员工进行交付。菜单加载数据库中的所有员工就好了,但是当我选择一个并单击添加时,我收到错误“转换错误设置值'(员工的哈希)'为'空转换器'。
here's the code for the dropdown menu:
这是下拉菜单的代码:
<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}" required="false">
<f:selectItem itemValue="#{null}" itemLabel="Selct One"/>
<s:selectItems value="#{deliveryPort.empList}" var="emp" label="# {emp.employeeId} #{ emp.nameFirst}"/>
</my:dropdown>
Any help would be greatly appreciated. Thank you
任何帮助将不胜感激。谢谢
回答by flash
You should do 2 things to avoid this error:
你应该做两件事来避免这个错误:
- Make sure your class
emp
is implementingequals()
andhashCode()
- Use an converter to convert your selected value before passing it to the backing bean
- 确保您的班级
emp
正在实施equals()
和hashCode()
- 使用转换器转换您选择的值,然后再将其传递给支持 bean
BalusC wrote a nice tutorialabout converters and how to use them.
BalusC 写了一个关于转换器和如何使用它们的很好的教程。
回答by Stefanos Kargas
I implemented the converter and equals() & hashCode() in Employee class (class of emp) according to the answer of @flash.
我根据@flash 的回答在Employee 类(emp 类)中实现了转换器和equals() & hashCode()。
Part of XHTML:
XHTML 的一部分:
<my:dropdown label="Employee ID" id="emp" value="#{deliveryPort.emp}" required="false">
<f:selectItem itemValue="#{null}" itemLabel="Selct One"/>
<s:selectItems value="#{deliveryPort.empList}" var="emp" label="# {emp.employeeId} #{ emp.nameFirst}"/>
<f:converter converterId="empConverter" />
</my:dropdown>
EmployeeConverter Class:
EmployeeConverter 类:
package mypackage.converters;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter("empConverter")
public class EmployeeConverter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
return value;
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
return value.toString();
}
}
Part of Employee Class (the class of emp, supposedly employeeId is String):
Employee类的一部分(emp的类,应该是employeeId是String):
public boolean equals(Object other)
{
return other instanceof Employee && (employeeId != null) ? employeeId.equals(((Employee) other).employeeId) : (other == this);
}
public int hashCode()
{
return employeeId != null ? this.getClass().hashCode() + employeeId.hashCode() : super.hashCode();
}
public String toString()
{
return "Employee[" + employeeId + "," + nameFirst + "]";
}
I had a similar problem. It worked for me.
我有一个类似的问题。它对我有用。