JSF 2 f validateBean

JSF 2 f validateBean


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

In this tutorial you will learn about how to use the JSF 2 f validateBean tag.

JSF 2 f validateBean

In this tutorial you will learn about how to use the JSF 2 f validateBean tag.

<f:validateBean> tag assigns the validation of local value to the Bean Validation API ( For Bean Validation tutorial click here). To validate the bean's local value some built in constraints are used, these constraints are provided in JSF 2.x.x with JSR 303 and is available in as annotation constraints (read from here). Wherever these annotations are used JSF validates that UIInput values accordingly.

Attributes of <f:validateBean>

  • validationGroups : This attribute is not required to use with this tag. But it can be used to specify the validation groups manually by a fully-qualified class name at the time of validating a particular component.
  • binding : This is not a required attribute but, is used to bound the expression to evaluate to BeanValidatior instance.

NOTE : In our previous examples I have used the JSF 2.0.2 version jar files, When I tried to do this example I have faces some problems perhaps in this version there is a bug for this tag so I have upgraded my JSF jar files and the problem has sorted out. In this example I have used the JSF 2.1.6 version as well as you will required some other JAR files for validating the bean, these are javax.validation and bean-validator.jar. To download click here or for the upgrade versions for JSF you can also visit http://javaserverfaces.java.net/download.html this site.

Example :

Here I am giving simple example which will demonstrate you about how to use the JSF validateBean. In this example at first I have created a JavaBeans class with the property name. And Uses the annotation @Size on the field name and the @NotEmpty on the getter method of the property. Then created the JSF pages for taking input and used the <f:validateBean> tag inside the <h:inputText> tag to validate the input value according to the annotation constraints used in the JavaBeans class.

Directory Structure

UserBean.java

package devmanuals;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

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

@Size(min=3, message="Enter minimum 3 characters!")
private String name;

@NotEmpty(message="Textbox can't be empty")
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">

<f:view>
<h:form>
<h:outputText value="Name"></h:outputText>
<h:inputText id="tx" value="#{userBean.name}"> 
<f:validateBean />
</h:inputText><br/>
<h:message for="tx" style="color:red"/><br/>
<h:commandButton value="submit" action="output"/> 
</h:form>
</f:view>
</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">

<f:view>
<h:outputText value="Your Name is : #{userBean.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>jsfValidateBean</display-name>
<welcome-file-list>
<welcome-file>input.jsf</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>
</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 will have to start your web server first and then used this link http://localhost:8080/jsfValidateBean/input.jsf into 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 will be as follows :

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

3. But when you will entered the less than 3 characters into the textbox then the output will be as follows :

4. But when you will entered the characters >= 3 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