Database And JSF Primer

Introduction

 
A database is a collection of information and most databases are organized in the tables. My SQL is a software package that is used to create retrieve, and update and delete the data. API is used for web applications to interact with the database to perform its task, and GUI is used to check the individual data items. The SQL standard specifies 27 basic statements with numerous variants. The basic structural elements of a database are rows, columns, tables, and indices.
 
To design a database for a web application, associate a table to Java class, which represents data.
 
The code specifies the Eshop application book categories in the Java class:
  1. package eshop.beans;    
  2.     
  3. public class Category    
  4. {    
  5.  private int id;    
  6.  private String name;    
  7.      
  8. public Category (int id, String name)    
  9. {    
  10.  this.id = id;    
  11.  this.name = name;    
  12. }    
  13.  public int getId ()    
  14. {    
  15.   return id;    
  16. }    
  17.  public void setId (int id)    
  18. {    
  19.  this.id = id;    
  20. }    
  21.  public String getName ()    
  22. {    
  23.  return name;    
  24. }    
  25.  public void setName (String name)    
  26. {     
  27.  this.name = name;    
  28. }    
  29. }    
To create a table to store the categories in the shop database the below query is used:
  1. create table shop.categories  (  
  2.  category_id integer not null auto_increment unique, category_name varchar (70) , primary key (category_id)  
  3. };  
DDL - Data Definition Language - statements used to define the DB structure.
 
DML - Data Manipulation Language - statements used to manage data.
 
DCL - Data Control Language - statements are used to control access to the data.
 
TCL - Transaction control Language - Statements are used to group together DML statements to logical transactions. 
 

JSF custom tags

 
The JSF application has a user interface called view which consists of a tree of UI component object. Components like button text field and table ID are used. 
  • Converters - Used to convert between the data type of components and application objects.
  • Listeners - The listener is used to handling events of a certain type and is registered with the component.
  • Events - The faceServlet fires the event by invoking the event notification method to the listener after the listener is registered with the component.
  • Validators - It used to examine the local value of the component and it conforms to a set of predefined rules.

JSF Application

 
Install the JSF jar files.
 
Create an application folder inside web apps with subfolders. 
 
Develop the JSP modules and backing java beans constitute the application and JSP modules contain JSF tags.
 
Write WEB-INF\web.xml file to describe the deployment of the application. 
 
Write a JSF configuration file named WEB-INF\faces-config.xml.
 
Top menu
  1. < f: subview id = "viewcart" >  
  2.     <h: panelGroup styleClass = "header" >  
  3.     <h: outpuText styleClass = "logo" value = "e-shopping center" />  
  4.     <h: commandLink styleClass = "cart" action = "showcart" immediate = "true" >  
  5.     <h: outputText value = "Show cart" />  
  6.     <h: graphicImage url = "/images/cart.gif" / >  
  7.     </h: commandLink>  
  8.     </h: panelGroup>  
  9. </f: subview>  
h:subview is an element which is equivalet to pair of braces and the content of the document doesn't conflict inside another jspx file.
 

Registering converter with the application

 
The converter is registered with the application by adding the below content to faces-config.xml and select the name inside the converter-id element and the class converter-class element.
  1. < converter>  
  2.     < converter-id> CCNumberConverter < /converter-id>  
  3.     < converter-class> eshop.converter.CCNumber < /converter-class>  
  4. </ converter>  

Validators

  • f:validateDoubleRange - It is used to validate a numeric input is within given range and it is applicable to values converterd to a double.
  • f:validateLength - It validates the length of input string within a range. 
  • f:validateLongRange - It validates a numeric input in a given range and it is applicable to vlues converted to long.
  • It is nested inside the h:input component to validate the validators accept the maximum attribute to set upper limit of the range.
Custom validation
 
The custom validator is implemented create an implementation which overrides the validate method and register the validator in faces-config-xml and refer to the validator in an attribute or component. The below code is added to faces-config.xml to register the validator with the application.
  1. < validator >  
  2.    < validator-id > CCExpiryValidator </validator-id >  
  3.    < validator-class > eshop.validator.CCExpiryValdator < / validator-class >  
  4. < /validator>  


Similar Articles