JSF view parameters

Starting with JSF 2.0, a new set of parameters is available. This set is named view parameters. These parameters are specified as metadata to the page and can be included in the generated URLs as you will see in this recipe.

Getting ready

We have developed this recipe with NetBeans 6.8, JSF 2.0, and GlassFish v3. The JSF 2.0 classes were obtained from the NetBeans JSF 2.0 bundled library.

How to do it...

The official API documentation describes a view parameter as an entity represented by the javax.faces.component.UIViewParameter component class that acts as a declarative binding (using an EL value expression) between a request parameter and a model property.

A view parameter is commonly specified in the f:metdata tag using the f:viewParam tag (we say that the parameters are specified as metadata to the page), as in the following example (notice that this parameter is defined in page2.xhtml—we will navigate to this page from page1.xhtml):

…
<f:metadata>
<f:viewParam id="id" name="viewParam" value="#{bean.bye}"/>
</f:metadata>
…

Now, we will "exploit" this view parameter from a h:link hyperlink. This hyperlink is defined in page1.xhtml like this:

…
<h:link includeViewParams="true" outcome="page2.xhtml"
value="HelloToYouByeToHer">
<f:param name="helloparam" value="#{bean.hello}"/>
</h:link>
…

Notice that we have set the includeViewParams attribute to true on h:link (this is true for h:button also). This will have a great effect because the UIViewParameters will be a part of the generated URL. You also may use the include-view-params attribute on the redirect element of a navigation case set to true to obtain the same effect.

The result of this component is listed next—even if you never activate the component. Note that this URL can be bookmarked from the first moment: http://localhost:8080/JSF_view_parameters/faces/page2.xhtml?helloparam=Adrian&viewParam=Mary.

The bean responsible for the values of helloparam and viewParam is:

package beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class Bean {
private String hello = "Adrian";
private String bye = "Mary";
public Bean() {
}
public String getBye() {
return bye;
}
public void setBye(String bye) {
this.bye = bye;
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}

Note

Keep in mind that the view parameters that are included in the generated URL will be those of the view being navigated to.

Now, going deeper into the view parameters world, we notice that JSF 2.0 process the view parameters using the standard post-back processing lifecycle, which allows us to attach converters and validators to them. For example, we indicate that our view parameter is required as shown next:

…
<f:metadata>
<f:viewParam id="id" name="viewParam" value="#{bean.bye}"
required="true" requiredMessage="This parameter is a must!"/>
</f:metadata>
…

Or here is a more complex example, with a validator attached:

…
<f:metadata>
<f:viewParam id="id" name="id" value="#{bean.property}"
required="true" requiredMessage="…" converterMessage="…"
validatorMessage="…">
<f:validateLongRange minimum="1"/>
</f:viewParam>
</f:metadata>
…

Note

Usage of f:metadata can be extended to Facelets templating features and view events, and is not specific only to view parameters. There's a lot more to view parameters than what was shown before.

How it works...

We can't say that the previous examples are self-explanatory, but we also can't explain here the secrets behind the scenes because we would then have a very large section. Anyway, what we can do is to make you aware that the view parameters provide information about how request parameters should be handled when a view is requested or linked to, which means that the view parameters are not rendered themselves. So, we say that they are part of the view's meta-model and described using metadata, f:metadata.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and it is named: JSF_view_parameters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset