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
In JSF you can do this by using a so-called view parameter. You declare these in the metadata
section of your Facelet:
This will grab the URL parameter id
from the request URL. E.g. if you request the page this appears on with localhost:8080/mypage.jsf?id=1
, then 1
will be handed to the yourObjectConverter
and whatever this converter returns will be set in yourBean.yourObject
.
Your backing bean will thus get the converted object. No need to pollute your backing bean over and over again with the same query code.
@ManagedBean
public class YourBean {
private SomeObject someObject;
public void setYourObject(SomeObject someObject) {
this.someObject = someObject;
}
}
If your backing bean is view scoped, you may want to use the OmniFaces variant of viewParam
instead, since otherwise it will needlessly convert after each postback (if your converter does a DB query, you definitely don't want this).
Working full examples:
Further reading: