Primefaces selectOneMenu转换器已调用但不起作用 [英] Primefaces selectOneMenu converter called but not working
问题描述
我查看了此和 转换器: 实体类被剪断(生成): 页面加载后,我可以看到以下日志语句: 因此将调用转换器并返回正确的值,但是在页面上它仍然是 com.xyz.Def [defid = 1] (下拉并正常显示) 转换器似乎正在运行,但是您并未发布整个 I've looked at the other questions this and this, etc, the problem is that my convert gets called but the values of selectOneMenu doesn't change. My entity class is generated, and has equals as well as hashCode and I would like not to change anything in it - if it gets regenerated then all changes will be lost (The work around is to change toString of the entity class). The XHTML code snipped: The Converter: The entity class snipped (this is generated): When the page loads, I can see the log statements as follow: Thus the converter gets called and returns the correct values but on the page it is still com.xyz.Def[ defid=1 ] (Drop down and normal) The converter seems to be working, but you didn't post the whole 这篇关于Primefaces selectOneMenu转换器已调用但不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!@FacesConverter("defConverter")
public class DefConverter implements Converter
{
private static final Logger LOG = Logger.getLogger(DefConverter.class.getName());
@EJB
private DefFacade defFacade;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string)
{
LOG.info("getAsObject: " + string);
try
{
return defFacade.findWithNFieldsWithValue("name", string, "=").get(0);
}
catch (Exception ex)
{
LOG.log(Level.SEVERE, "Error while fetching Def for " + string, ex);
}
return null;
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object obj)
{
LOG.info("getAsString obj class: " + obj.getClass().getName());
if(obj instanceof Def)
{
Def def = (Def)obj;
LOG.info("getAsString def name: " + def.getName());
return def.getName();
}
else
{
StringBuilder sbError = new StringBuilder("The object of class ");
sbError.append(obj.getClass().getName()).append(" is not of Def");
throw new ClassCastException(sbError.toString());
}
}
}
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "defid")
private Long defid;
...
@Override
public int hashCode()
{
int hash = 0;
hash += (defid != null ? defid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object)
{
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Def))
{
return false;
}
Def other = (Def) object;
if ((this.defid == null && other.defid != null) || (this.defid != null && !this.defid.equals(other.defid)))
{
return false;
}
return true;
}
getAsString obj class: com.xyz.Def
getAsString def name: Name 1
getAsString obj class: com.xyz.Def
getAsString def name: Name 2
getAsString obj class: com.xyz.Def
getAsString def name: Name 3
<p:selectOneMenu>
代码,尤其是<f:selectItems>
.它应该看起来像这样<p:selectOneMenu id="defid"
value="#{abcController.selected.defid}"
converter="defConverter">
<f:selectItems value="#{abcController.defs}" var="def"
itemLabel="#{def.name}" itemValue="#{def.defId}" />
</p:selectOneMenu>
itemLabel
负责打印显示的值.<p:selectOneMenu id="defid"
value="#{abcController.selected.defid}"
converter="defConverter">
@FacesConverter("defConverter")
public class DefConverter implements Converter
{
private static final Logger LOG = Logger.getLogger(DefConverter.class.getName());
@EJB
private DefFacade defFacade;
@Override
public Object getAsObject(FacesContext fc, UIComponent uic, String string)
{
LOG.info("getAsObject: " + string);
try
{
return defFacade.findWithNFieldsWithValue("name", string, "=").get(0);
}
catch (Exception ex)
{
LOG.log(Level.SEVERE, "Error while fetching Def for " + string, ex);
}
return null;
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object obj)
{
LOG.info("getAsString obj class: " + obj.getClass().getName());
if(obj instanceof Def)
{
Def def = (Def)obj;
LOG.info("getAsString def name: " + def.getName());
return def.getName();
}
else
{
StringBuilder sbError = new StringBuilder("The object of class ");
sbError.append(obj.getClass().getName()).append(" is not of Def");
throw new ClassCastException(sbError.toString());
}
}
}
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "defid")
private Long defid;
...
@Override
public int hashCode()
{
int hash = 0;
hash += (defid != null ? defid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object)
{
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Def))
{
return false;
}
Def other = (Def) object;
if ((this.defid == null && other.defid != null) || (this.defid != null && !this.defid.equals(other.defid)))
{
return false;
}
return true;
}
getAsString obj class: com.xyz.Def
getAsString def name: Name 1
getAsString obj class: com.xyz.Def
getAsString def name: Name 2
getAsString obj class: com.xyz.Def
getAsString def name: Name 3
<p:selectOneMenu>
code, in particular <f:selectItems>
. It should look something like this<p:selectOneMenu id="defid"
value="#{abcController.selected.defid}"
converter="defConverter">
<f:selectItems value="#{abcController.defs}" var="def"
itemLabel="#{def.name}" itemValue="#{def.defId}" />
</p:selectOneMenu>
itemLabel
is responsible for printing displayed values.