错误的值被发送到转换器JSF

时间:2013-12-18 07:54:07

标签: java jsf xhtml

使转换器,equals和JSF代码工作的问题的最终解决方案。

播放器是一个具有一些属性的播放器bean。该服务访问播放器并将播放器信息写入数据库(除了点:):

<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中

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();
    }

2 个答案:

答案 0 :(得分:2)

getAsString中,您要返回String个对象,而在getAsObject中,您尝试将值解析为LongidValue = Long.parseLong(value);任何意义。

您应该在getAsString中返回正确的 ID ,以便在getAsObject中获取。

更新

E.g。 您的getAsString方法应如下所示:

public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
    if (value == null || value.equals("")) {
        return "";
    }
    else {
        return String.valueOf(((ServicePlayer) value).getId());
    }
}

答案 1 :(得分:1)

应该是这样的。

       <f:selectItems value="#{servicePlayer.allPlayers}"
             var="n"
             itemValue="#{n}"
             itemLabel="#{n.combinedName}"
             itemLabelEscaped="true"/>
        </h:selectOneListbox>

itemValue 应该是播放器对象(n), NOT n.stringID,因为h:selectOneListbox的值是播放器对象。

的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方法。

另见:

希望这有帮助。