JSF 2 f validateDoubleRange

JSF 2 f validateDoubleRange


Posted in : Java Posted on : June 18, 2012 at 11:13 AM Comments : [ 0 ]

In this tutorial we will discuss about the JSF 2 core tag validateDoubleRange.

JSF 2 f validateDoubleRange

In this tutorial we will discuss about the JSF 2 core tag validateDoubleRange.

<f:validateDoubleRange> tag is used to specify the range of double on the closest component. When this tag is used within which component an instance of DoubleRangeValidator is instantiated on that component. Using this tag you can fix the range of the input value.

Attributes of JSF 2 <f:validateDoubleRange>

  • disabled : This attribute is not required to use with this tag. This tag evaluates to java.lang.Boolean which presence determines that whether the DoubleRangeValidator has to be enabled on the component or not.

  • maximum : This is not a required attribute to be used with this tag. This tag evaluates to java.lang.Double, the value defined for this attribute specifies the maximum value can be given to the enclosing component.

  • minimum : This is not a required attribute to be used with this tag. This tag evaluates to java.lang.Double, the value defined for this attribute specifies the minimum value can be given to the enclosing component.

  • binding : This is not a required attribute to be used with this tag. This tag is used to bound with the expression which evaluates to an instance of DoubleRangeValidator.

  • for : This is not a required attribute to be used with this tag. This attribute is used to refer one of the composite component's value within which this tag is nested.

Example :

Here we are giving a simple example for you which will demonstrate about how to use JSF 2 f validateDoubleRange tag in an application. This example has been created using Eclipse IDE. So all the JSF pages and Java classes are created in according to the Eclipse directory structure. So, lets come on our topic the example of <f:validateDoubleRange>. In this example at first I have created a Java Bean class with the property named 'amount' declared as data type 'double' and its setter getter method. Then created JSF page where used this tag for validating the min and max range of the input value inside the <h:intputText> tag which is used to take the value as input.

Directory Structure

AmountBean.java

package devmanuals;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="validateDoubleBean")
@RequestScoped
public class AmountBean {

double amount;

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}
}

inputPage.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<head>
<title>JSF 2 f validateDoubleRange</title>
</head>
<body>
<f:view>
<h:form>
Enter the value
<h:inputText id="amt" label="amount" value="#{validateDoubleBean.amount}" > <br/>
<f:validateDoubleRange minimum="10" maximum="20.50" /> 
</h:inputText>
<h:message for="amt" style="color:red" /> <br/>
<h:commandButton value="submit" />
</h:form>
<br/>
<h:outputText value="Entered amount is : #{validateDoubleBean.amount}" />
</f:view>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfValidateDoubleRange</display-name>
<welcome-file-list>
<welcome-file>inputPage.xhtml</welcome-file> 
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

How to Run

To run this example at first start your web server then use the following link http://localhost:8080/jsfValidateDoubleRange/inputPage.xhtml in the address bar of your web browser.

Output :

When you will execute the above example you will get the output as follows

When you will input the value like the given into image then the output will be as follows :

The above error message(amount: Validation Error: Specified attribute is not between the expected values of 10 and 20.5) is given because the given input is outside the range defined for validating. The defined range is shown into the error message between which the value must be input for rendering response.

So, when you will give the input in the text box between the defined range (as given into the image below) then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics