Wrong value being sent to converter JSF

半腔热情 提交于 2020-01-06 04:14:05

问题


Final solution of the problem for getting the converter, equals and JSF code to work.

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>

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

}

In Player

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

回答1:


It should be like this.

       <f:selectItems value="#{servicePlayer.allPlayers}"
             var="n"
             itemValue="#{n}"
             itemLabel="#{n.combinedName}"
             itemLabelEscaped="true"/>
        </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
    }
}

EDIT: Please note that the Player MUST implement the equals and the hashcode methods.

See Also:

  • Validation error value is not valid.

Hope this helps.




回答2:


in getAsString you are returning a String object, and in getAsObject you are trying to parse the value as Long: idValue = Long.parseLong(value); which does not make any sense.

You should return the proper id in getAsString so you can get it in getAsObject.

Updated

E.g. Your getAsString method should look like :

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


来源:https://stackoverflow.com/questions/20652484/wrong-value-being-sent-to-converter-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!