Can anyone clarify how we can use in general, or a in real world example, this snippet?
<f:metadata>
<f:viewParam id="id" value="#{bean.id}" />
<f:viewAction action="#{bean.init}" />
</f:metadata>
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Process GET parameters
The
<f:viewParam>manages the setting, conversion and validation of GET parameters. It’s like the<h:inputText>, but then for GET parameters.The following example
does basically the following:
id.required,validatorandconverterattributes and nest a<f:converter>and<f:validator>in it like as with<h:inputText>)#{bean.id}value, or if thevalueattribute is absent, then set it as request attribtue on nameidso that it’s available by#{id}in the view.So when you open the page as
foo.xhtml?id=10then the parameter value10get 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.Performing business action on GET parameters
You can use the
<f:viewAction>for this.with
The
<f:viewAction>is however new since JSF 2.2 (the<f:viewParam>already exists since JSF 2.0). If you can’t upgrade, then your best bet is using<f:event>instead.This is however invoked on every request. You need to explicitly check if the request isn’t a postback:
When you would like to skip "Conversion/Validation failed" cases as well, then do as follows:
Using
<f:event>this way is in essence a workaround/hack, that’s exactly why the<f:viewAction>was introduced in JSF 2.2.Pass view parameters to next view
You can "pass-through" the view parameters in navigation links by setting
includeViewParamsattribute totrueor by addingincludeViewParams=truerequest parameter.which generates with the above
<f:metadata>example basically the following linkwith the original parameter value.
This approach only requires that
next.xhtmlhas also a<f:viewParam>on the very same parameter, otherwise it won’t be passed through.Use GET forms in JSF
The
<f:viewParam>can also be used in combination with "plain HTML" GET forms.With basically this
@RequestScopedbean:Note that the
<h:message>is for the<f:viewParam>, not the plain HTML<input type="text">! 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).See also: