0

I'm having some problems with JSF navigation and passing parameters; this is the scenario:

Three pages: artists, albums, tracks

For each page a bean: ArtistBean, AlbumBean, TrackBean (session scoped)

The artist page contains a list of artists, each artistname is a link and when clicking it you navigate to the album page which lists the albums by that artist. Similarly, when clicking an album, you navigate to the track page with a list of tracks for that album.

The links looks as follows:

Page artist: <h:commandLink action="#{albumBean.showAlbums(artist)}" value="#{artist.name}" />
Page album:  <h:commandLink action="#{trackBean.showTracks(album)}" value="#{album.name}" />

The beans looks as follows:

AlbumBean: public String showAlbums(Artist artist){
              [generate list of albums, fetched by page "albums" using getAlbumList()]
              return "Albums";
           }

           public List<Album> getAlbumList(){
              return albumList;
           }

TrackBean: public String showTracks(Album album){
              [generate list of tracks, fetched by page "tracks" using getTrackList()]
              return "Tracks";
           }

           public List<Track> getTrackList(){
              return trackList;
           }

I'm not sure if this is an "OK" way to handle navigation; calling a backing bean for a page that has not yet bean displayed. I also get some weird behaviour, mainly that the links to show tracks only works if I click twice, whereas the links to show albums always work when just clicking once.

Thanks in advance!

nivis
  • 913
  • 3
  • 17
  • 34
  • I'm not sure what you're concretely asking. You're basically asking "Here is my code, is it okay?" which is a non-constructive question and thus offtopic for Stack Overflow and better suited in a generic discussion forum. As to your double-click problem (which *seems* to be your real concrete problem), read point 7 of http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked – BalusC Jun 04 '12 at 15:43
  • My reason for asking about if it was "an ok way to handle navigation" is connected to the fact that my links didn't work as expected. I should have phrased it differently to make that more clear. It is still a question though and I'm interested in knowing if that way of passing parameters to another bean is sound or if may give me problems. – nivis Jun 04 '12 at 19:00
  • After a lot of trial-and-error I got it to work when removing form bindings to a request scoped bean that I've used for setting form messages. I don't know why that caused the problems though. – nivis Jun 08 '12 at 14:55

1 Answers1

1

I'm not sure if it is wrong what you are doing. But I find it complicated. A more straightforward solutions seems to me the following:

You have 3 beans and 3 pages. Obviously artist bean is called in artist page and so on.

Navigating between pages is done via links and you add a parameter. On Album bean, read the passed parameter and fill you list of albums on basis of that parameter (you could use name or an id as key value)

Same goes for track info.

That way you don't need to have the beans on session scope. Better to use view scope.

roel
  • 2,005
  • 3
  • 26
  • 41