Can anyone clarify how we can use in general, or a in real world example, this snippet?
The
, but then for GET parameters.
The following example
does basically the following:
id
.required
, validator
and converter
attributes and nest a
and
in it like as with
)#{bean.id}
value, or if the value
attribute is absent, then set it as request attribtue on name id
so that it's available by #{id}
in the view.So when you open the page as foo.xhtml?id=10
then the parameter value 10
get set in the bean this way, right before the view is rendered.
As to validation, the following example sets the param to required="true"
and allows only values between 10 and 20. Any validation failure will result in a message being displayed.
You can use the
with
public void onload() {
// ...
}
The
is however new since JSF 2.2 (the
already exists since JSF 2.0). If you can't upgrade, then your best bet is using
This is however invoked on every request. You need to explicitly check if the request isn't a postback:
public void onload() {
if (!FacesContext.getCurrentInstance().isPostback()) {
// ...
}
}
When you would like to skip "Conversion/Validation failed" cases as well, then do as follows:
public void onload() {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
// ...
}
}
Using
this way is in essence a workaround/hack, that's exactly why the
was introduced in JSF 2.2.
You can "pass-through" the view parameters in navigation links by setting includeViewParams
attribute to true
or by adding includeViewParams=true
request parameter.
which generates with the above
example basically the following link
with the original parameter value.
This approach only requires that next.xhtml
has also a
on the very same parameter, otherwise it won't be passed through.
The
can also be used in combination with "plain HTML" GET forms.
...
...
...
With basically this @RequestScoped
bean:
private String query;
private List results;
public void search() {
results = service.search(query);
}
Note that the
is for the
, not the plain HTML ! Also note that the input value displays
#{param.query}
when #{bean.query}
is empty, because the submitted value would otherwise not show up at all when there's a validation or conversion error. Please note that this construct is invalid for JSF input components (it is doing that "under the covers" already).