2

I am creating a CRUD app in JSF.enter image description here I have used MovieName and RespectiveID.The Moviename is in CommandLink tag.Now I want that if someone clicks on the movie it will show the detail for respective movie.If it would be PHP I can easily append the ID in parameter movieDetail.php?id=12 and use it for querying DB.But as far as I know in JSF we use actionLIstener and call a bean function for it. I dont know what i would write in the bean function apart from query to show the result..Anyone who can help me with this Thanks

2 Answers2

1

Create a view movieDetail.jsf and while outputting the movie names create links by appending movie ids. e.g. movieDetail.jsf?id=12 .

In movieDetail.jsf add <f:viewParam> tag and set the id value to a bean property. Then add

<f:event type="preRenderView" listener="#{bean.listener}"> and populate the bean with movie details.

preRenderView event is fired just before the view is rendered. So, this is the perfect time to fill the bean with movie details. In preRenderView event the listener method will make use of the id property to query the database and find the details of the movie.

Use the two tags inside <f:metadata> in your movieDetail.jsf :

<f:metadata>
<f:viewParam name="id" value="#{bean.id}"/>
<f:event type="preRenderView" listener="#{bean.listener}"/> 
</f:metadata>

When user accesses your view, say, movieDetail.jsf?id=12 the id value 12 is set to bean's id property. Before the view is rendered the listener method i.e in this case public void listener() of bean is called which fills the bean with movie details.

Narayan Subedi
  • 1,343
  • 2
  • 20
  • 46
Sandeep Panda
  • 723
  • 6
  • 9
  • that means I dont need to add actionListener on the link? –  Dec 27 '12 at 15:15
  • No, you don't have to. Just output html links that look like this: Ace Goldfinger . All you have to do is create a view called movieDetail.jsf that will make use of passed id and find details of the movie. Take a look at this link: http://balusc.blogspot.in/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters – Sandeep Panda Dec 27 '12 at 15:20
  • You dont need to use h:commandLink. Just use h:link. e.g. . – Sandeep Panda Dec 27 '12 at 15:49
  • that doesn't worked out ,as I can't receive the varable ID in next page –  Dec 27 '12 at 20:10
0

You should definately use a separate JSF page as Sandeep suggests, but I think you might benefit from the use of a datatable to display the list of movies. Check out this link about how to work with them.

You should create a simple class which holds the type of data for each movie that you want displayed in the list (name and id). Then you can create a List of those object types and populate the datatable using that list. Then using the binding attribute, you can bind the datatable to an HtmlDataTable object in your backing bean and get a reference to the row that the user clicks on. In the method handling the user click, you can set the data you need for the specified movie and return the name of the view for displaying the single movie.

  • this article is by balus C of SO.. I just loved that guy.. Thanks for the share –  Dec 27 '12 at 16:56