我在项目中使用Hibernate,Spring和JSF(Primefaces)。我想用简单的POJO(实体)创建一个选项列表。这就是我创造的:
<p:pickList id="pickList" converter="#{groupConverter}" value="#{adminUsersMB.groups}" var="group"
itemLabel="#{group.name}" itemValue="#{group}" >
<f:facet name="sourceCaption">Available groups</f:facet>
<f:facet name="targetCaption">Users groups</f:facet>
</p:pickList>
我的转换器是:
@RequestScoped
@FacesConverter(forClass=Group.class, value="groupConverter")
public class GroupConverter implements Converter {
@ManagedProperty(name="groupService", value="#{groupService}")
@Getter @Setter
GroupService groupService;
@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
try {
return groupService.getGroupByName(Integer.parseInt(arg2));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
return ((Group) arg2).getId().toString();
}
}
当然我将自定义转换器添加到了faces config:
<converter>
<converter-id>groupConverter</converter-id>
<converter-class>pl.proedims.users.component.GroupConverter</converter-class>
</converter>
但是当我提交表单时,我得到了例外:
> SEVERE: javax.el.PropertyNotFoundException: /admin/user.xhtml @73,66
> itemLabel="#{group.name}": Property 'name' not found on type java.lang.String
答案 0 :(得分:1)
问题解决了。我只需输入:
@Component("groupConverter")
public class GroupConverter implements Converter {
@Autowired
GroupService groupService;
(...)