JSF 2 f validateRequired

JSF 2 f validateRequired


Posted in : Java Posted on : June 20, 2012 at 6:43 PM Comments : [ 0 ]

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

JSF 2 f validateRequired

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

<f:validateRequired> tag is a newly added into the JSF 2 core tag library. This tag enforces the UIInput for non empty. Within which UIComponent this tag is nested it make sure that this value can not be the empty. This tag has the same behavior as the the required attribute of a UIInput is set to true. To use the validateRequired tag in JSF to validate the empty field use,

<context-param> 
 <param-name>
  javax.faces.VALIDATE_EMPTY_FIELDS 
 </param-name> 
 <param-value>true</param-value> 
</context-param>

in web.xml file to enable the validation of empty fields.

Attributes of <f:validateRequired>

  • disabled : This attribute is not a required to be used with this tag. This attribute evaluates to a java.lang.Boolean which value specifies whether this tag will be enabled or not for the component on the JSF page.

  • binding : This is not a required attribute to be used with this tag but, is used to bound the expression to evaluate to javx.faces.validator.RequiredValidator instance.
  • 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 I am giving a simple example which will demonstrate you about how to use the <f:validateRequired> JSF 2 core tag. In this example I have created a JavaBeans with the property 'name' and its setter getter method. Then created the JSF pages for taking the input for the name and for validating them as required using the <f:validateRequired> tag.

Directory Structure

NameRequired.java

package devmanuals;

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

@ManagedBean(name="nameBean")
@RequestScoped
public class NameRequired {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
} 
}

input.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 validateRequired</title>
</head>
<body>
<f:view>
<h:form>
<h:outputText value="Enter Name : "/><br/>
<h:message for="tx" style="color:red" /><br/>
<h:inputText id="tx" value="#{nameBean.name}" label="name">
<f:validateRequired />
</h:inputText><br/><br/>
<h:commandButton value="submit" action="output"/>
</h:form>
</f:view>
</body>
</html>

output.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 f validateRequired output</title>
</head>
<f:view>
<h:outputText value="Your Name is : #{nameBean.name}"/>
</f:view>
</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>jsfValidateRequired</display-name>
<welcome-file-list>
<welcome-file>input.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 this example

To run this example you may either import the WAR file (provided at the end of this example) or may copy the above code and paste them into appropriate folder and add the required jar files and then to execute select your project -> Right Click -> go to Run As -> Run on Server.

Output :

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

1. The Main page will be looked as :

2. When you will not give any value into the provided textbox and clicked on submit button then the output will be as follows :

3. But when you will give the value in the textbox (here I have given 'deepak') 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