Java Server Face Introduction
Java Server Face is a framework and solution for rapid development of web applications. He changed the process-response mechanism based on Java web application, using an event-driven processing mechanism. This provides possible possible to quickly develop web applications on the Java platform. The JSF framework simplifies the Web Form validity check, the request parameter resolution, status management, multi-threaded support. Developers simply implement specific event processors and transaction logic (called Handlers and Components in JSF, respectively.
When developing with a JSF framework, no inheritance of any API of the JSF is required, the relationship between the components is implemented through an XML (Faces-Config.xml) configuration file. His implementation is similar to the Spring framework introduced in the past.
Comparison of Java Server Face and MVC Architecture
Different from the current MVC architecture now, JSF does not separate views and logical transactions in design structures, but depends on the specific implementation of programming staff. For example, Handlers and Components can be implemented in a class in the actual development, so that the view layer and the logic processing layer are closely coupled. It is recommended that the program designers should be strictly required to separate Handlers and Components when performing JSF projects.
The following is a comparison of MVC (with Struts as an example) and JSF framework in structure design.
As can be seen from the above figure, the development structure of MVC and JSF is very different. MVC mainly focuses on the development of loose coupling of each layer; JSF is focused on incident processing. From the application, the division of the MVC attached to the structure and JSF is committed to providing a base solution for rapid development. The author recommends that the two architectures cannot be compared, and the actual development and application choice should be combined.
Technology implementation of Java Server Face
The implementation of the JSF framework depends on the following technologies: Java Tag Lib, IOC TYPE2 (SETTER AND GETTER), XML
The representation of the JSF uses TAG LIB technology to process the web object
Associate Form Object and specific Handler and Component using IOC Type2 and XML
Sample of Java Server Face
Example Application Demand Instructions: Implement a user management function, requires the view layer, the logic layer, and the data access layer to reduce the couplings of each layer.
The following figure is an illustration of an example, wherein the data layer uses DAO Hibernate technology.
Userform code:
Package jsfdemo.forms;
/ **
* Created Date: 2004-8-19
* @Author genstone
*
* /
Public Class Userform
{
PRIVATE STRING UserName;
PRIVATE Long UserId;
PRIVATE STRING Password;
/ **
* @Return Returns the username.
* /
Public string getUsername ()
{
Return UserName;
}
/ **
* @Param Username The UserName To Set.
* /
Public void setusername (String Username)
{
THIS.USERNAME = UserName;
}
/ **
* @Return Returns the password.
* /
Public string getpassword ()
{
Return Password;
}
/ **
* @Param Password The Password to Set.
* /
Public void setpassword (string password) {
this.password = password;
}
/ **
* @Return Returns the userid.
* /
Public long getUserid ()
{
Return UserId;
}
/ **
* @Param Userid the userid to set.
* /
Public void setUserid (long userid)
{
THIS.USERID = UserId;
}
}
UserHandler code:
Package jsfdemo.handlers;
Import jsfdemo.components.basecomponent;
/ **
* Created Date: 2004-9-20
* @Author genstone
*
* /
Public class userHandler
{
Private basecomponent component;
/ **
* @Return Returns the Component.
* /
Public BaseComponent getcomponent ()
{
Return Component;
}
/ **
* @Param Component The Component To Set.
* /
Public void setcomponent (BaseComponent Component)
{
this.Component = Component
}
Public void save ()
{
Component.save ();
}
Public void delete ()
{
Component.delete ();
}
Public void modify ()
{
Component.modify ();
}
}
UserComponent code:
Package jsfdemo.components;
Import net.sf.hibernate.hibernateException;
Import jsfdemo.beans.userinfo;
Import jsfdemo.daos.basedao;
Import jsfdemo.forms.userform;
Import jsfdemo.util.valueobejctutil;
IMPORT Util.logger;
/ **
* Created Date: 2004-9-20
* @Author genstone
*
* /
Public Class UserComponent Implements Basecomponent
{
Private userform userform = null;
PRIVATEDAO DAO = NULL;
/ **
* @Return Returns the userform.
* /
Public userform getuserform ()
{
Return Userform;
}
/ **
* @Param Userform The Userform to set.
* /
Public Void SetUserform (Userform Userform)
{
THIS.USERFORM = Userform;
}
Public boolean save ()
{
UserInfo Info = Valueobejctutil.getPersistObject ((userform) Userform;
Try
{
// Insert Into Database
Info.userid = (long) DAO.SAVE (INFO);
Catch (HibernateException E)
{
// Todo Auto-Generated Catch Block
Logger.getLogger (this.getclass ()). Error (E.GetMessage ());
}
// Return Forward Page
Return True;
}
Public Boolean Delete ()
{
UserInfo Info = Valueobejctutil.getPersistObject ((userform) Userform;
Try
{
// Insert Into Database
DAO.DELETE (INFO);
}
Catch (HibernateException E)
{
// Todo Auto-Generated Catch Block
Logger.getLogger (this.getclass ()). Error (E.GetMessage ());
}
// Return Forward Page
Return True;
}
Public boolean modify ()
{
UserInfo Info = Valueobejctutil.getPersistObject ((userform) Userform;
Try
{
// Insert Into Database
Dao.update (Info);
}
Catch (HibernateException E)
{
// Todo Auto-Generated Catch Block
Logger.getLogger (this.getclass ()). Error (E.GetMessage ());
}
// Return Forward Page
Return True;
}
/ **
* @Return Returns the Dao.
* /
Public Basedao getdao ()
{
Return DAO;
}
/ **
* @Param Dao the Dao to set.
* /
Public void setdao (Basedao DAO)
{
THIS.DAO = DAO;
}
}
Userdao code:
Package jsfdemo.daos;
IMPORT Util.logger;
Import net.sf.hibernate.hibernateException;
/ **
* This class has been automaticly generated by Hibernate Synchronizer.
* For more information or documentation, Visit the Hibernate Synchronizer Page
* at http://www.binamics.com/hibernatesync or Contact Joe Hudson at joe@binamics.com.
*
* This is the object class thing relates to the TBL_USER TABLE.
* Any Customizations Belong Here.
* /
Public Class Userinfodao Extends BaseuserinFodao
{
PRIVATE STRING HibernateConfig = NULL;
Public userinfodao ()
{
}
/ **
* @Return Returns The HibernateConfig.
* /
Public string gethibernateConfig ()
{
Return HibernateConfig;
/ **
* @Param HibernateConfig The HibernateConfig to set.
* /
Public void sethibernateConfig (String HibernateConfig)
{
THIS.HIBERNATECONFIG = HibernateConfig;
Try
{
this.initialize (this.hibernateConfig);
}
Catch (HibernateException E)
{
// TODO automatically generates a Catch block
Logger.getLogger (this.getclass ()). Error (E.GetMessage ());
E.PrintStackTrace ();
}
}
}
Profile: /Web-inf/faces-config.xml
XML Version = "1.0" encoding = "UTF-8"?>
managed-bean>
managed-protety>
managed-bean>
managed-protety>
managed-protety>
managed-bean>
managed-protety>
managed-bean>
faces-config>
Profile: /Web-inf/web.xml
Xml Version = "1.0" encoding = "ISO-8859-1"?>
PUBLIC "- // Sun microsystems, Inc.//dtd Web Application 2.2 // en"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
context-param>
servlet>
servlet-maping>
web-app>
User_add.jsp code:
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Taglib Uri = "http://java.sun.com/jsf/html" prefix = "h"%>
<% @ Taglib URI = "http://java.sun.com/jsf/core" prefix = "f"%>
.input {height: 18px; Border: Solid 1px # 3979bd; Background-Color: # E7f3ff; Color: # 000000} .button {
FONT-SIZE: 12PX;
Border-width: 1px;
Border-color: # d4d0c8;
CURSOR: POINTER;
Background-color: # dee0e0;
}
Style>
HEAD>