错误的值被发送到转换器JSF [英] Wrong value being sent to converter JSF
问题描述
最终获得转换器,equals和JSF代码的问题的解决方案.
Final solution of the problem for getting the converter, equals and JSF code to work.
播放器是具有一些属性的播放器bean.该服务访问播放器并将播放器信息写入数据库(点之外):
The Player is a Player bean with a few properties. The service access the Player and writes player info to the database (besides the point :)
<h:selectOneListbox size="1" value="#{player}"
converter="playerConverter" id="playerList">
<f:selectItems value="#{servicePlayer.allPlayers}"
var="n"
itemValue="#{n}"
itemLabel="#{n.combinedName}"
itemLabelEscaped="true"/>
</h:selectOneListbox>
在ServicePlayer中
In ServicePlayer
public List<Player> getAllPlayers() {
if (factory == null) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
EntityManager em = factory.createEntityManager();
Query q = em.createQuery("select t from Player t");
List<Player> players = q.getResultList();
....
@FacesConverter(value = "playerConverter")
public class PlayerConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null) {
return null;
}
long idValue;
try {
idValue = Long.parseLong(value);
}
catch (NumberFormatException ex)
{
return null;
}
ServicePlayer servicePlayer = context.getApplication()
.evaluateExpressionGet(context, "#{servicePlayer}",
ServicePlayer.class);
Player player = servicePlayer.getPlayerByID(idValue);
return player;
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((Player)value).getStringID());
}
}
}
在播放器中
@Override
public boolean equals(Object other) {
return (id != null && other != null && getClass() == other.getClass())
? id.equals(((Player) other).getId())
: (other == this);
}
@Override
public int hashCode() {
return (id != null)
? (getClass().hashCode() + id.hashCode())
: super.hashCode();
}
推荐答案
应该是这样的.
<f:selectItems value="#{servicePlayer.allPlayers}"
var="n"
itemValue="#{n}"
itemLabel="#{n.combinedName}"
itemLabelEscaped="true"/>
</h:selectOneListbox>
itemValue 应该是播放器对象(n),否 n.stringID,因为h:selectOneListbox
的值是播放器对象.
The itemValue should be the player object (n), NOT n.stringID, since the value of the h:selectOneListbox
is a player object.
getAsObject
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value) {
if (value == null) {
return null;
}
long idValue;
try {
idValue = Long.parseLong(value);
}
catch (NumberFormatException ex)
{
return null;
}
ServicePlayer servicePlayer = context.getApplication()
.evaluateExpressionGet(context, "#{servicePlayer}",
ServicePlayer.class);
Player player = servicePlayer.getPlayerByID(idValue);
return player;
}
getAsString
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
if (value == null || value.equals("")) {
return "";
} else {
return String.valueOf(((Player)value).getStringID());
//or what ever the id is
}
}
编辑: 请注意,播放器必须实现equals和hashcode方法.
EDIT: Please note that the Player MUST implement the equals and the hashcode methods.
另请参见:
希望这会有所帮助.
这篇关于错误的值被发送到转换器JSF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!