Is there a way to display a specific JSF page based on the request URL?
Let’s say I have a JSF page “details.xhtml“. The managed bean “detailsBean” has a list of objects where each object has its own ID. Now if a user requests the page “../details.xhtml?id=1“, the list should be queried for an object with ID 1 and the resulting details page of this object should be displayed.
I already wrote a converter implementation class which can convert from object to ID and vice versa, but I don’t know how to use it properly. Do I have to work through the JAX-RS specification for this to work or is there a more simple solution?
You can achieve this with plain JSF with the following steps
Capture the ID in the request to determine what object is being queried for in your
DetailsBeanfrom the request parameter. There are many ways to achieve this, one of which is adding the following annotation to your managed bean (this is currently only permitted for a@RequestScopedbean, see why here).The annotation above will capture the
idparameter from the request and assign it to therequiredObjectIdvariable.Using the captured Id, setup your object in your bean in a
@PostConstructmethodThe object retrieved should be assigned as an instance variable of your managed bean
In your view, you could then reference the queried object that has been setup in the backing bean
If your bean is in a scope broader than the request scope, you’ll need a combination of constructs to cleanly pull that request parameter before view rendering.
Capture the request parameter within the JSF view itself using
Due to the nature of JSF Lifecycle processing, doing the above alone may not make the value available for your use in time for object setup. You could use the following instead.
What we’ve done here is specify a method (that captures the
id) in the backing bean that must be executed before the view is rendered, ensuring that theidparameter is available as at the time you need it. Proceed with step 3, only if you’re using<f:event/>above.In the backing bean, you now define the
setObjectIdmethodNote that the above option is generally a work around/hack and not a clean solution as such