I can't get the bean to update when I choose another name from the list
<h:selectOneMenu id="playerMenu" value="#{player.combinedName}">
<f:selectItems value="#{serviceHCP.allPlayers}" />
<f:ajax execute="playerMenu" render="playerMenu" />
If I use above method I will always end upp with player == null
If I use it without the f:ajax I will always end up with the same "player". But of course I want to have the one selected in the menu item.
I will then pass it to a bean
<h:commandButton value="Visa lista" action="#{serviceSeries.getSeriesForPlayerInfo(club.name, player, serieType.serie_type, calendarBean) }">
</h:commandButton>
public List<Player> getAllPlayers() {
if (factory == null) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
EntityManager em = factory.createEntityManager();
//Order by the matchdate.
//Query q = em.createQuery("select t from DBTest t");
Query q = em.createQuery("select t from Player t");
List<Player> players = q.getResultList();
for (Player aPlayer : players) {
System.out.println(aPlayer);
}
System.out.println("Size: " + players.size());
em.close();
return players;
}
You have to write a custom jsf converter to convert your Player object. Your converter will most likely be..
@FacesConverter("playerConverter")
public class PlayerConverter implements Converter {
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
// Convert Object to unique String representation for display.
return String.valueOf(((Player) value).getId());
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
// Convert submitted unique String representation back to Object.
return new Player(Long.valueOf(value));
}
}
And your select one should look like,
<h:selectOneMenu id="playerMenu" value="#{XXXXXX.player}">
<f:selectItems value="#{serviceHCP.allPlayers}" var="player" itemLabel="#{player.name}" itemValue="#{player.id}"/>
<f:converter converterId="playerConverter" />
</h:selectOneMenu>
If you have a problem using the Converter, the why don't you use java.util.Map to hold the Player data.
Using map may also be helpful in other operations.
JSF h:selectOneMenu
supports java.util.Map too.
If you have Map then K will become the itemLabel and V will become the itemValue.
You can load the Player Objects to as Values to Map.
Map<String,Player> playerMap = new LinkedHashMap<>();
Player selectedPlayer;
then..,
<h:selectOneMenu value="#{bean.selectedPlayer}">
<f:selectItems values="#{bean.playerMap}"/>
</h:selectOneMenu>
If you want Orders Map collection to preserve Insertion order use LinkeHashMap
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.