java Primefaces selectOneMenu 转换器被调用但不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28742165/
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
Primefaces selectOneMenu converter called but not working
提问by TungstenX
I've looked at the other questions thisand 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).
我已经查看了其他问题this和this等,问题是我的 convert 被调用,但 selectOneMenu 的值不会改变。我的实体类已生成,并且具有 equals 和 hashCode,我不想更改其中的任何内容-如果重新生成,则所有更改都将丢失(解决方法是更改实体类的 toString)。
The XHTML code snipped:
截取的 XHTML 代码:
<p:selectOneMenu id="defid"
value="#{abcController.selected.defid}"
converter="defConverter">
The Converter:
转换器:
@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());
}
}
}
The entity class snipped (this is generated):
实体类剪断了(这是生成的):
...
@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;
}
When the page loads, I can see the log statements as follow:
当页面加载时,我可以看到如下日志语句:
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
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)
因此转换器被调用并返回正确的值,但在页面上它仍然是com.xyz.Def[ defid=1 ](下拉和正常)
回答by Predrag Maric
The converter seems to be working, but you didn't post the whole <p:selectOneMenu>
code, in particular <f:selectItems>
. It should look something like this
转换器似乎在工作,但您没有发布整个<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
is responsible for printing displayed values.
itemLabel
负责打印显示值。
回答by Swathi
Try the following code
试试下面的代码
<p:selectOneMenu id="defid" value="#{abcController.selected.def}"
converter="defConverter">
<f:selectItems value="#{abcController.defs}" var="def"
itemValue="#{def}" itemLabel="#{def.name}" />
</p:selectOneMenu>
Converter is used to convert between a complex Java object and a String representation. For this you need to specify the whole def object as item value instead. You should also ensure that #{abcController.selected.def} refers to a def property, not some Long property representing the def ID.
Converter 用于在复杂的 Java 对象和 String 表示之间进行转换。为此,您需要将整个 def 对象指定为项目值。您还应该确保 #{abcController.selected.def} 指的是 def 属性,而不是一些代表 def ID 的 Long 属性。