0

Here's my XHTML code:

<h:selectOneMenu id="combo" value="#{TeamsHinzufuegenBean.selectedLeagueId}">
    <f:selectItems value="#{TeamsHinzufuegenBean.leagues}"
                   var="league" itemValue="#{league.id}"
                   itemLabel="#{league.name}"/>
</h:selectOneMenu>

And my bean:

@ManagedBean(name = "TeamsHinzufuegenBean")
@ViewScoped
public class TeamsHinzufügenBean implements Serializable{

    private static final long serialVersionUID = 1L;
    private List<League> leagues;
    private ArrayList<Team> teams = new ArrayList<Team>();
    private String teamname;
    private int selectedLeagueId=1;

    @PostConstruct
    public void init() {
        leagues = Database.getInstance().getAllLeagues();

        for(League l : leagues)
            System.out.println(l);
    }

    public List<League> getLeagues() {
        return leagues;
    }

    public void setLeagues(List<League> leagues) {
        this.leagues = leagues;
    }

    public int getSelectedLeagueId() {
        return selectedLeagueId;
    }

    public void setSelectedLeagueId(int selectedLeagueId) {
        this.selectedLeagueId = selectedLeagueId;
    }

    public ArrayList<Team> getTeams() {
        return teams;
    }

    public void setTeams(ArrayList<Team> teams) {
        this.teams = teams;
    }

    public String getTeamname() {
        return teamname;
    }

    public void setTeamname(String teamname) {
        this.teamname = teamname;
    }
}

The league-class has an attribute id but if I output the value of selectedLeagueId, it is always 0.

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • looks like the default of the `selectedLeagueId`. You should troubleshoot your bean's lifecycle and setters. Post more of your bean here – kolossus Apr 03 '15 at 19:06
  • Do you change the view/navigate to another page? – Konstantin Yovkov Apr 03 '15 at 19:08
  • 1
    According to `itemValue="#{league.id}"` and `itemLabel="#{league.name}"`, it is clear that `List` contains a list of composite/complex objects. In which case, you need to implement a custom converter and attach it to `` (`` or whatever). `` will then look something like this ``. – Tiny Apr 03 '15 at 19:15
  • no i dont change the view.. – Lukas Buchacher Apr 03 '15 at 19:15
  • 2
    @Tiny is incorrect: you do not need to implement a converter. Your `selectOneMenu` is bound to an `int`, and your `itemValue` is also set to an `int`, so you do not need to implement a converter ([reference](http://stackoverflow.com/questions/2434902/how-to-dynamically-create-a-fselectitem-list)). – DavidS Apr 04 '15 at 03:15
  • can you post the rest of your xhtml code, and your scenario? – tt_emrah Apr 04 '15 at 18:29

1 Answers1

0

Check if getAllLeagues() contains objects that have an id and that it is correctly set

rexxar
  • 1,671
  • 1
  • 21
  • 27