Introduction to Java Server Faces(JSF

)
Deepak Goyal Vikas Varma Sun Microsystems

Objective

Understand the basic concepts of Java Server™ Faces[JSF] Technology.

2

Agenda
   

What is and why JSF? Architecture Overview UI Component Model Development Steps

JavaServer™ Faces (JSF) Framework Is… A server side user interface component framework for Java™ technology-based web applications 4 .

What is JSF? ● A specification and reference implementation for a web application development framework – – – – – Components Events Validators & converters Navigation Back-end-data integration 5 .

Why JSF? (page 1) ● ● ● ● ● ● ● MVC for web applications Clean separation of roles Easy to use Extendable Component and Rendering architecture Support for client device independence Standard Huge vendor and industry support 6 .

Why JSF? (page 2) ● JSP and Servlet – No built-in UI component model No built-in UI component model No built-in event model for UI components No built-in state management for UI components No built-in support of multiple renderers Not a standard (despite its popularity) ● Struts (I am not saying you should not use Struts) – – – – – ● Struts and JSF can be used together 7 .

2) JSF API Servlets (2.3) 8 .How the JSF Specification Fits In JSF App JSF App JSF Tags JSP (1.

Agenda     What is and why JSF? Architecture Overview UI Component Model Development Steps .

JSF Architecture [MVC] Server JSF Page Desktop Browser HTML HTML RenderKit Front ctrl JSF Page WML RenderKit App Backend Phone WML 10 .

Passes controls to Lifecycle 1.Request processing Lifecycle FacesServlet 2.Creates FacesContext Lifecycle FacesContext 3. Process FacesContext in various phase .

Request Processing Lifecycle Phases Faces Request Reconstitute Component Tree Apply Request Values Process Validations Faces Response Render Response Invoke Application Update Model Values 12 .

Request Processing Lifecycle Response Complete Response Complete Faces Request Reconstitute Component Tree Apply Request Values Render Response Response Complete Process Events Process Validations Process Events Response Complete Faces Response Render Responder Process Events Invoke Application Process Events Update Model Values Conversion Errors / Render Response Validation / Conversion Errors / Render Response 13 .

Render response phase 14 .Invoke application phase 6.Apply request values phase 3.Process validations phase 4.Reconstitute component tree phase 2.Update model values phase 5.Request Processing Lifecycle Phases 1.

Agenda     What is and why JSF? Architecture Overview UI Component Model Development Steps .

User Interface Component Model ● ● ● ● ● UI components Event handling model Conversion and Validation model Rendering model Page navigation support 16 .

UIForm. UIPanel. UIOutput UIGraphic.UI Components   UIComponent/UIComponentBase  Base class for all user interface components UICommand. UIInput. UISelectMany. UIParameter UISelectBoolean.userNumber}"/> . UISelectOne Standard UIComponent Subclasses     Example: <h:inputText id="userNo" value="#{UserNumberBean.

Validators and Converters  Validators—Perform correctness checks on UIInput values    Register one or more per component Enqueue one or more messages on errors Standard implementations for common cases Output: Object to String Input: String to Object Standard implementations for common cases  Converters—Plug-in for conversions:    .

today” <f:validator_length minimum=”6” maximum='10” /> .Converters and Validators Example: Converters: <h:input_text valueRef=”testingBean.today” convertor=”DateTime”/> Validators: <h:input_text valueRef=”testingBean.

Rendering Model  Renderers-Adapt components to a specific markup language   Decoding Encoding Map component classes to component tags Is a custom tag library Basic HTML RenderKit  RenderKits—Library of Renderers    .

Events and Listeners   Follows JavaBeans™ Specification design and naming patterns Standard events and listeners  ActionEvent—UICommand component activated by the user  ValueChangedEvent—UIInput component whose value was just changed .

Navigation Model  Application developer responsibility  Defined in Application configuration file (Facesconfig.xml)  Navigation rules   Determine which page to go. Navigation case .

jsp</to-tree-id> </navigation-case> </navigation-rule> .Navigation Model <navigation-rule> <from-tree-id>/login.jsp</to-tree-id> </navigation-case> <navigation-case> <from-outcome>failed</from-outcome> <to-tree-id>/error.jsp</from-tree-id> <navigation-case> <from-outcome>success</from-outcome> <to-tree-id>/menu.

Agenda     What is and why JSF? Architecture Overview UI Component Model Development Steps .

Create Pages using UI component and core tags 4. Configure web. Develop model objects which hold the data 2. Add model objects (managed bean) declarations to Application Configuration File faces-config.xml . Define Page Navigation in faces-config.Steps in Development Process 1.xml 5.xml 3.

request  JSF keeps the bean's data in sync with the UI . session.Step1: Develop model Objects (Managed Bean)      The model (M) in MVC A regular JavaBeans with read/write properties May contain application methods and event handlers Use to hold data from a UI (page) Creation and lifetime is managed by JSF runtime  application.

Step 2.LoginFormBean </managed-bean-class> <managed-bean-scope> request </managed-bean-scope> </managed-bean> . Managed Bean Declaration (Faces-config.xml) 01 02 03 04 05 06 07 08 09 10 11 <managed-bean> <managed-bean-name> LoginFormBean </managed-bean-name> <managed-bean-class> myapp.

Step 3: Create JSF Pages     Must include JSF tag library  HTML and core tags All JSF tags must enclosed between a set of view tag Use JSF form and form component tags  <h:input_text> not <input type=”text”>  <h:command_button> not <input type=”submit”> May include validators and event listeners on any form components .

logon”/> <h:command_button type=”reset” label=”Reset”/> </h:panel_grid> </f:form> </f:view> .jsp) 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 <f:view> <f:form formName=”logonForm”> <h:panel_grid columns=”2”> <h:output_text value=”Username:”/> <h:input_text id=”username” length=”16” valueRef=”logonBean.password”/> <h:command_button type=”submit” label=”Log On” actionRef=”logonBean.Sample JSF™ Page (login.username”/> <h:output_text value=”Password:”/> <h:input_secret id=”password” length=”16” valueRef=”logonBean.

) { public String getUserName(.. public void setUserName(..LoginFormBean </managed-bean-class> LoginFormBean.jsp <h:input_text id=”userName” valueRef=”LoginFormBean...Binding UI to Managed Bean login.java public class LoginFormBean .xml <managed-bean> <managed-bean-name> LoginFormBean </managed-bean-name> <managed-bean-class> myapp.userName”/> faces-config..) { ..

jsp</from-tree-id> <navigation-case> <from-outcome>success</from-outcome> <to-tree-id>/menu.jsp</to-tree-id> </navigation-case> </navigation-rule> <navigation-rule> <from-tree-id>/login.Step 4: Define Page Navigation Rules (Faces-config.jsp</to-tree-id> </navigation-case> </navigation-rule> .xml) 01 02 03 04 05 06 07 08 09 010 011 012 013 014 015 <navigation-rule> <from-tree-id>/login.jsp</from-tree-id> <navigation-case> <from-outcome>failure</from-outcome> <to-tree-id>/error.

application.xml </param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class> javax.webapp.Step 5: Configure (web.xml) 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 <context-param> <param-name> javax.Faces Servlet Mapping --> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> .faces.faces.FacesServlet</servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> <!-.CONFIG_FILES </param-name> <param-value>/WEB-INF/faces-config.

jar WEB-INF/lib/jstl.xml WEB-INF/faces-config.xml WEB-INF/classes/LoginFormBean.JSF Application directory structure WEB-INF/web.jar WEB-INF/lib/commons-beanutils.jar WEB-INF/lib/commons-collections.class login.jar WEB-INF/lib/commons-logging.jar WEB-INF/lib/standard.jar WEB-INF/lib/jsf-ri.jar .jar WEB-INF/lib/commons-digester.jar WEB-INF/lib/jsf-el.jsp Required Jars: WEB-INF/lib/jsf-api.

Summary    JSF: Server side UI component framework MVC Developing application in JSF .

jcp.com/reading/index.com/j2ee/javaserverfaces/ http://www.jsfcentral.org/en/jsr/detail?id=127 .Reference    http://www.sun.html http://java.

com .Q&A deepak.com vikas.goyal@sun.varma@sun.

Sign up to vote on this title
UsefulNot useful