[英]JSF 2.0 "Conversion Error setting value 'courseID' for 'null Converter' on Primefaces Picklist
I am getting the following error when I want to save the data of my picklist 当我要保存选择列表的数据时出现以下错误
'Group [id=null, groupName=TEST, distinguishedName="distinguishedNameForGroup"' für 'null Converter'.
'Group [id = null,groupName = TEST,distinguishedName =“ distinguishedNameForGroup”“,为” null Converter“。
This is the converter I use for it @FacesConverter(value="groupConverter", forClass=Group.class) public class GroupConverter implements Converter{ 这是我使用的转换器@FacesConverter(value =“ groupConverter”,forClass = Group.class)公共类GroupConverter实现了Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
if (value.trim().equals("")) {
return null;
}
Object ret = null;
if (component instanceof PickList) {
Object dualList = ((PickList) component).getValue();
DualListModel<?> dl = (DualListModel<?>) dualList;
for (Object o : dl.getSource()) {
String id = ((Group) o).getDistinguishedName();
if (value.equalsIgnoreCase(id)) {
ret = o;
break;
}
}
if (ret == null)
for (Object o : dl.getTarget()) {
String id = ((Group) o).getDistinguishedName();
if (value.equals(id)) {
ret = o;
break;
}
}
}
return ret;
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null || value.equals("")) {
return "";
} else {
String str = "";
if (value instanceof Group) {
str = ""
+ ((Group) value)
.getDistinguishedName();
}
return str;
}
}
} }
And the code for the pickList: 以及pickList的代码:
<p:pickList
value="#{bean.groups}" var="group"
itemLabel="#{group.groupName}" itemValue="#{group}"
converter="groupConverter">
</p:pickList>
I don't understand the error, because in the web I found that the converter is not called, but definitely is. 我不明白该错误,因为在Web上我没有找到该转换器,但肯定是。 I am running out of ideas.
我的想法不多了。 There is no null value or anything.
没有空值或任何东西。 The Group.class have an equal and hashcode method.
Group.class具有相等和哈希码方法。
Update: I found out that the selectOneMenu makes the error. 更新:我发现selectOneMenu导致错误。 This is the code for it.
这是它的代码。 There is no converter, that might be the problem.
没有转换器,可能是问题所在。 Testing it.
测试一下。
<p:selectOneMenu id="abteilungsleiterrolle"
value="#{bean.object.group}">
<f:selectItems value="#{bean.source}" var="q"
itemValue="#{q}" itemLabel="#{q.groupName}"/>
</p:selectOneMenu>
Try this 尝试这个
@ViewScope
@FacesConverter(value="pickListConverter")
public class PickListConverter implements Converter{
Map<String, Object> tmp;
@PostConstruct
public void init() {
tmp = new HashMap<String, Object>();
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return tmp.get(value);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
tmp.put(value.toString(), value);
return value.toString();
}
}
}
Whit this converter only make sure that your toString() ( NameClass and fields and id ) for example "Group[id=" + id +"]" are well format but this converter works for any class 此转换器只能确保您的toString() ( NameClass和fields和id )(例如“ Group [id =“ + id +”]”)格式正确,但是此转换器适用于任何类
And remember change converter name: 并记住更改转换器名称:
<p:pickList
value="#{bean.groups}" var="group"
itemLabel="#{group.groupName}" itemValue="#{group}"
converter="pickListConverter">
Part of error message " für 'null Converter'.
" indicates that there is no valid converter found. 错误消息“
für 'null Converter'.
”的一部分表示没有找到有效的转换器。 You can try folowing: 您可以尝试以下方法:
List<Group>
JSF should find matching converter by class as you have specified forClass=Group.class
, but i think you don`t have to give your converter a name List<Group>
JSF应该按您为forClass=Group.class
指定的类按类别找到匹配的转换器,但是我认为您不必给转换器一个名称 @ManagedBean
and specify el for bean eg #{groupConverter}
@ManagedBean
对其进行注释,并为bean指定el,例如#{groupConverter}
If converter method is called, but error still persists, then converter not found ("null Converter") error can be produced by other jsf component. 如果调用了Converter方法,但错误仍然存在,则其他jsf组件可能会产生未找到的转换器错误(“空Converter”)。 Keep in mind that if you specify value="#{bean.group}" where bean.group is Group.class object and omit converter, then JSF will lookup for converter annotated with forClass="Group.class".
请记住,如果您指定value =“#{bean.group}”,其中bean.group是Group.class对象并省略了转换器,那么JSF将查找以forClass =“ Group.class”注释的转换器。 Here if you made converter as bean you have to specify converter for each component.
在这里,如果将Converter转换为bean,则必须为每个组件指定Converter。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.