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>
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>
On
Send params from View to an other View, from Sender View to Receiver View use viewParam and includeViewParams=true
In Sender
Sender.xhtml
<f:metadata>
<f:viewParam name="ID" value="#{senderMB._strID}" />
</f:metadata>
“includeViewParams=true” in return String of click button event
Click button fire senderMB.clickBtnDetail(dto) with dto from senderMB._arrDataSender.xhtml
<p:dataTable rowIndexVar="index" id="dataTale"value="#{senderMB._arrData}" var="dto">
<p:commandButton action="#{senderMB.clickBtnDetail(dto)}" value="見る"
ajax="false"/>
</p:dataTable>
In senderMB.clickBtnDetail(dto) we assign _strID with argument we got from button event (dto), here this is Sender_DTO and assign to senderMB._strID
Sender_MB.java
public String clickBtnDetail(sender_DTO sender_dto) {
this._strID = sender_dto.getStrID();
return "Receiver?faces-redirect=true&includeViewParams=true";
}
The link when clicked will become http://localhost:8080/my_project/view/Receiver.xhtml?*ID=12345*
In Recever
Receiver.xhtml
<f:metadata><f:viewParam name="ID" value="#{receiver_MB._strID}"/></f:metadata>
It will get param ID from sender View and assign to receiver_MB._strID
Receiver.xhtml
<f:event listener="#{receiver_MB.preRenderView}" type="preRenderView" />
into f:metadata tag
Receiver.xhtml
<f:metadata>
<f:viewParam name="ID" value="#{receiver_MB._strID}" />
<f:event listener="#{receiver_MB.preRenderView}"
type="preRenderView" />
</f:metadata>
Now we want to use this param in our read database method, it is available to use
Receiver_MB.java
public void preRenderView(ComponentSystemEvent event) throws Exception {
if (FacesContext.getCurrentInstance().isPostback()) {
return;
}
readFromDatabase();
}
private void readFromDatabase() {
//use _strID to read and set property
}
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: