1) Introduction
This article introduces about the various Core Tags that are available in JSF. Tags represent some set of Actions that will be executed in the Server. This article assumes the reader to have a fair bit of knowlege in Java Server Faces. If not, then they can visit the Introductory Article in JSF in javabeat. For more information and resources about Java Server Faces, readers can also Jsf Articles Page visit here. This article will start off with the Major Classification of Core JSF Tags and then will explain in depth about the various different types of Tags that are available in each category. Wherever and whenever possible, Code Snippets are given to make the understanding of the Tags much clearer.
2) Core Tag Library
The Core Tags that are available in Java Server Faces are categorized as follows. Here the categorization is given based on the Functionality of the Tags. They are,
- Validator Tags
- Convertor Tags
- Listener Tags
- View Tags
- Select Tags
- Facet Tag
- Miscellaneous Tags
Following sections explains the various above mentioned Tags in greater detail.
3) Validator Tags
3.1) Introduction
In JSF, Data from the Clients, usually Browser, has to be validated before being processed by the Server Web Application. JSF has a number of Built-in Validations and also it comes with a Powerful Validation Framework for plugging-in Custom Validators. Bindings between JSF UI Components and Validation Logic can be done with ease with the help of JSF Core Tags. Four JSF Core Tags are available for doing Validation on the JSF UI Components. They are,
- Validate Length Tag
- Validate Long Range Tag
- Validate Double Range Tag
- Validator Tag
Let us see the various Validation Tags in greater detail.
3.2) Validate Length Tag
Validation pertaining to Data between Boundaries is a normal requirement for almost any kind of Web Application. For example, a common case includes "Password must be of [8-16] characters length". Validation Length Tag as identified by <f:validateLength> is used to specify the maximum and the minimum characters, a JSF UI Component can accept.
Following is the syntax definition of the Validate Length Tag,
<f:validateLength minimum = "minRange" maximum = "maxRange">
</f:validateLength>
In the above syntax, the value for the minimum and the maximum attributes must be numbers, which tells the Boundary Range allowed for a particular UI Component. For example, if a number of characters allowed for a username Text Component must be greater than 10 and less than 15, then we can have the following code which will achieves this,
<h:inputText value = "#{UserBean.userName}" id = "userNameTextField" required="true">
<f:validateLength minimum = "10" maximum = "15"/>
</h:inputText>
In the above code, UserBean refers to a Managed Bean being defined and declared in the web.xml file and userName is the property of the UserBean class. The attribute 'id' of the <h:inputText> specifies the identifier of the Text Field UI Component and it must be unique among the other Components in the Form.
3.3) Validate Long Range Tag
This Tag is used to do Validations on JSF UI Components whose value is expected to fall between certain integer (long) values. Common cases will be restricting the age of a user between 1 and 40. The Validate Long Range Tag is identified by <f:validateLongRange> tag.
Following is the syntax definition of the Validate Long Range Tag,
<f:validateLongRange minimum = "minLongValue" maximum = "maxLongValue">
</f:validateLongRange>
In the above syntax, the 'minimum' and the 'maximum' attributes of Validate Long Range specifies the minimum and the maximum of the long value. Assuming that the birth-date of an Year Text-Field UI Component must fall between 1960 and 1985, then we can have the following code to achieve this goal,
<h:inputText value = "#{UserBean.birthYear}" id = "birthYearTF" required = "true">
<f:validateLongRange minimum = "10" maximum = "15"/>
</h:inputText>
3.4) Validate Double Range Tag
This is the same the Validate Long Range Tag except that it operates on floating Data rather than Integer Data. UI Components that accepts floating values can use this Tag to perform Range Validations. The syntax for the following Tag is given by,
<f:validateDoubleRange minimum = "minDoubleValue" maximum = "maxDoubleValue">
</f:validateDoubleRange>
In the above syntax declaration, the 'minimum' and the 'maximum' attributes refer to the lower and the upper range of double value, a JSF UI Component can accept. For example, the following example restrict the Text UI Component to have values between 1000.00 and 100000.00.
<h:inputText value = "#{UserBean.amount}" id = "amountTextField" required = "true">
<f:validateDoubleRange minimum = "1000.00" maximum = "100000.00"/>
</h:inputText>
3.5) Validator Tag
For performing Customized Validations on UI Components, one can prefer using this Validator Tag. Examples may include validating whether the entered email-id is in the appropriate format (username@domain.com) or whether the stock symbol is available in the database. The following is the syntax for the Validator Tag.
<f:validator validatorId = "IdForValidator">
</f:validator>
In the above syntax, the value for the 'validatorId' attribute refers to a name which must be previously defined in the web.xml file. For example, if we want to do a Phone-Number Validation for a Text Component, then we would have written a class called PhoneNumberValidator which implements the javax.faces.validator.Validator interface and then overriding the Validator.validate() method. Following is the how the Class may looks like,
PhoneNumberValidator.java
package myvalidators;
import javax.faces.validator.*;
public class PhoneNumberValidator implements Validator{
public PhoneNumberValidator() {
}
public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
String strValue = (String)value;
if (strValue.length() != 10){
throwException("Number of phone digits must be 10");
}
}
}
Then, this Customized Validator Component must be registered to the Web Application by making the following entry in the faces-config.xml file.
faces-config.xml
<webapp>
<validator>
<validator-id>PhoneNumberValidator</validator-id>
<validator-class>myvalidators.PhoneNumberValidator</validator-class>
</validator>
</web-app>
Then the defined Validator Id in the faces-config.xml file, in the form of <validator-id> has to be referenced in UI Component like this,
<h:inputText value = "#{UserBean.phoneNumber}" id = "phoneNumberTF" required="true">
<f:validator validatorId = "PhoneNumberValidator"/>
</h:inputText>
|