JSF 2 f validateLength

JSF 2 f validateLength


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

This section describes how to validate the length of input text using JSF 2 f validateLength tag.

JSF 2 f validateLength

This section describes how to validate the length of input text using JSF 2 f validateLength tag.

<f:validateLength> tag is used to validate the length of the component that the text can be entered into the defined range. This tag can be nested within the UIComponent, to the closest component within which this tag is nested applied the LengthValidator validation by registering the instance of LengthValidator class. Using this tag you can specified the length of the text of the component.

Attributes of <f:validateLength>

  • disabled : This is not a required attribute to use with this tag. This attribute evaluates to a java.lang.Boolean, that determines whether the LengthValidator has to be enabled on the component or not.
  • maximum : This is not a required attribute for this tag. This attribute evaluates to java.lang.Integer which is used to define the allowable maximum length of the component that it can contain.
     
  • minimum : This is not a required attribute for this tag. This attribute evaluates to java.lang.Integer which is used to define the allowable minimum length of the component that it must contain.
  • 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 LengthValidator.
  • 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.

Directory Structue

UserBean.java

package devmanuals;

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

@ManagedBean(name="userBean")
@RequestScoped
public class UserBean {

private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} 
}

login.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 validateLength</title>
</head>
<body>
<f:view>
<h:form>
Enter userId : <br/>
<h:inputText id="uId" label="userId" value="#{userBean.name}" >
<f:validateLength maximum="10" for="uId" />
</h:inputText><br/>
<h:message for="uId" style="color:red" /> <br/>
Enter password<br/>
<h:inputSecret id="psw" label="password" value="#{userBean.password}">
<f:validateLength minimum="6" for="psw" />
</h:inputSecret><br/>
<h:message for="psw" style="color:red" /> <br/>
<h:commandButton value="submit" />
</h:form>
</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>jsfValidateLength</display-name>
<welcome-file-list>
<welcome-file>login.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

First you should have start your web server then the following link http://localhost:8080/jsfValidateLength/login.xhtml should be use 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 enter the value (as given into the image below) and if they breaks the allowable length defined for the respective components (textboxes) 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