JSF 2 f validateLongRange

JSF 2 f validateLongRange


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

In this tutorial you will learn about the JSF 2 f validateLongRange tag.

JSF 2 f validateLongRange

In this tutorial you will learn about the JSF 2 f validateLongRange tag.

<f:validateLongRange> tag is used to specify the range of long integer value on the closest component. When this tag is nested inside the UIcomponent an instance of LongRangeValidator is instantiated on the closest component. Using this tag you can specified the range of the input value.

Attributes of <f:validateLongRange>

  • disabled : This attribute is not required to use with this tag. This tag evaluates to java.lang.Boolean which presence determines that whether the LongRangeValidator 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.Long, 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.Long, 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 LongRangeValidator.
  • 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 validateLongRange 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 start to create the example of <f:validateLongRange>. In this example at first I have created a Java Bean class with the property named 'noOfEmp' declared as data type 'long' and its setter getter method. Then created JSF page where nested 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

EmpBean.java

package devmanuals;

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

@ManagedBean(name="validateLongBean")
@RequestScoped
public class EmpBean {

long noOfEmp;

public long getNoOfEmp() {
return noOfEmp;
}

public void setNoOfEmp(long noOfEmp) {
this.noOfEmp = noOfEmp;
}
}

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 validateLongRange</title>
</head>
<body>
<f:view>
<h:form>
Enter Number of Employee <br/>
<h:inputText id="emp" label="Number Of Employee" value="#{validateLongBean.noOfEmp}" > <br/>
<f:validateLongRange minimum="1" maximum="20" /> 
</h:inputText><br/>
<h:message for="emp" style="color:red" /> <br/>
<h:commandButton value="submit" />
</h:form>
<br/>
<h:outputText value="Number of Employee : #{validateLongBean.noOfEmp}" />
</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>jsfValidateLongRang</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 you would have to started your web server at first then the following link http://localhost:8080/jsfValidateLongRang/inputPage.jsf would have to use in the address bar of your web browser.

Output :

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

1. The first screen (main page) will be as follows :

2. When you will entered the value (as given in the image below) in the text box then the output will be as follows :

In the above image error occurred due to the input value exceeded the defined range. So if you give the input greater than 20 then this error message will be displayed every time and the render response phase will not be completed.

3. When you will give the input within the defined range 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