HTML and CSS Reference
In-Depth Information
Not
■
It is important to know that
<f:viewParam>
causes a
UIViewParameter
to be attached as metadata for the
current view.
UIViewParameter
extends
UIInput
, which means that any actions that one would normally take on a
UIInput instance are valid for instances of this class. So you can attach converters, validators, and value change listeners
to the
<f:viewParam>
tag.
As shown in the previous tip, we can add both converter and validators to
<f:viewParam>
tag. Let's see how we
can validate the view parameters in
car.xhtml
page. Let's modify the
Car
managed bean by adding a new attribute to
describe the car number as shown in Listing 4-29.
Listing 4-29.
Modifying Car Managed Bean
@ManagedBean
@RequestScoped
public class Car {
//...
private Long number;
//...
public Long getNumber() {
return number;
}
public void setNumber(Long number) {
this.number = number;
}
}
In order to mandates that all of
Car
attributes are mandatory, then as we do with other
EditableValueHolder
components, we can set
required
attribute to
true
. In order to validate that
Car
number attribute is a valid number
within a specific numeric range, we can use
<f:validateLongRange/>
inside
<f:viewParam>
tag. Listing 4-30 shows
how we can validate the view parameters in
car.xhtml
page.
Listing 4-30.
Modified car.xhtml Page to Utilize Validation
<html xmlns="
http://www.w3.org/1999/xhtml
"
xmlns:ui="
http://java.sun.com/jsf/facelets
"
xmlns:h="
http://java.sun.com/jsf/html
"
xmlns:f="
http://java.sun.com/jsf/core
"
>
<f:metadata>
<f:viewParam name="model" value="#{car.model}"
required="true"
requiredMessage="You need to specify car model"/>
<f:viewParam name="color" value="#{car.color}"
required="true"
requiredMessage="You need to specify car color"/>
