Pregunta

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

¿Fue útil?

Solución 2

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top