Introduction toJSF
-
Upload
softserve -
Category
Technology
-
view
1.795 -
download
1
description
Transcript of Introduction toJSF
Introduction toJSF
Objective
Understand the basic concepts of Java Server Faces (JSF) Technology.
Agenda
1. What is JSF and why JSF?2. Architecture Overview3. UI Component Model4. Summary of new features in JSF 25. Development Steps
JSF (JavaServer Faces) Framework Is…
A server side user interface component framework for Java technology-based web
applications
F-35 JSF Joint Strike Fighter
Ok, but what JSF really is?
A specification and reference implementation for a web application development framework
• Components• Events• Validators & converters• Navigation• Back-end-data integration• Localization• Error Handling
But why JSF?
• Standard• Huge vendor and industry support• MVC for web applications• Clean separation of roles• Easy to use• Extendable Component and Rendering
architecture• Support for client device independence
Agenda
1. What is JSF and why JSF?2. Architecture Overview3. UI Component Model4. Summary of new features in JSF 25. Development Steps
JSF Architecture
AppBackend
FrontCtrl
HTMLRenderer
kit
WMLRenderer
kit
DesktopBrowser
Phone
JSF Page
JSF Page
HTML
Server
WML
Request Processing Lifecycle Phases
RestoreView
ApplyRequestValues
ProcessValidations
Request
UpdateModel Values
InvokeApplication
RenderResponse
Response
Component tree retrieved
• Submitted form values stored in component
• Component values are converted
Component valuesvalidated
Component valuesbound to backing bean
• Events handled• Methods invoked• Navigation calculated
Component values are updated from backing bean properties
System-level phase
Application-level phase
Agenda
1. What is JSF and why JSF?2. Architecture Overview3. UI Component Model4. Summary of new features in JSF 25. Development Steps
User Interface Component Model
• UI components• Event handling model• Conversion and Validation model• Rendering model• Page navigation support
UI Components
• UIComponent/UIComponentBase– Base class for all user interface components
• Standard UIComponent Subclasses– UICommand, UIForm, UIOutput– UIGraphic, UIInput, UIPanel, UIParameter– UISelectBoolean, UISelectMany, UISelectOne
• Example <h:inputText id="Key" value="#{message.key}" required="true"
rendered="#{resBean.editModeRow}"/>
Validators
• Validators – pluggable validation on UIInput values• JSF provides implementation for common classes
(String reg. exp. validator, decimal range validator etc.)• Can apply one or more per component• Easy to create custom validators
• Implement interface and • public void validate(FacesContext context, UIComponent
component, Object value) throws ValidatorException;
• Example <h:inputText value="#{message.key}" required="true" rendered="#{resBean.editModeRow}“>
<f:validateLength maximum="15" minimum="6"/></h:inputText>
Converters
• Converters – pluggable conversion mechanism• JSF provides implementation for common classes
(Integer, Date etc.)• Easy to create custom converters• Implement interface and
• Output: Object to String• Input: String to Object
• Example <h:inputText id="Key" value="#{testBean.today}"
converter=“CustomDateConverter"/>
Events and Listeners
• Standard events and listeners• ActionEvent—UICommand component activated by
the user• ValueChangedEvent—UIInput component whose
value was just changed• System events• Possibility to work with PhaseEvents and
PhaseListeners• Global and component system events -
UIComponent.subscribeToEvent() or <f:event> tag.
Navigation Model
• Application developer responsibility• Defined in Application configuration file (faces-
config.xml)• Or implicitly (since JSF 2.0)
• Navigation rules• Determine which page to go• Navigation case
Navigation Model Sample
<navigation-rule><from-view-id>/wizard/item.jsp</from-view-id><navigation-case><from-outcome>wizardIndex</from-outcome><to-view-id>/wizerd/start.jsp</to-view-id></navigation-case><navigation-case><from-outcome>goBack</from-outcome><to-view-id>/wizard/cat3.jsp</to-view-id></navigation-case>
</navigation-rule>
Agenda
1. What is JSF and why JSF?2. Architecture Overview3. UI Component Model4. Summary of new features in JSF 25. Development Steps
What’s new?
• Facelets View Declaration Language• Annotation based configuration• Standardized Ajax• Implicit Navigation• Scopes (conversation, view, flash, custom)• Composite Components• Bean Validation Support• Error Handling• more - resource loading, improved GET support, behaviors,
tree visiting, system events and many more (out of scope)
Facelets, Annotations, Scopes
• Facelets are used by default instead of JSP.• Annotation based configuration• @ManagedBean, @...Scoped, @FacesConverter,
@FacesValidator etc.• New scopes• View – state is preserved for the view (URL).• Conversation and Flash.• Custom
Standardized Ajax
• Two ways to use• JavaScript API - jsf.ajax.request()• f:ajax tag
• Examples<h:selectOneMenu value="#{dynamicProperties.targetClass}" >
<f:ajax event="change" execute="@form" render="@form" /><f:selectItems value="#{dynamicProperties.targetClasses}" />
</h:selectOneMenu>
<h:commandButton value="Count"onclick="jsf.ajax.request(this.id, event, {execute: this.id, render: 'out1'}); return false;"/>
Composite Components
• Define<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite"><h:head></h:head><h:body>
<composite:interface> <composite:attribute name="who"/></composite:interface>
<composite:implementation><h:outputText value="Hello, #{cc.attrs.who}!"/>
</composite:implementation> </h:body></html>
• Use<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html" xmlns:greet="http://java.sun.com/jsf/composite/greet">
<greet:hello who="World"/> </html>
Navigation, Bean Validation Support, Error Handling
• Implicit Navigation.1. check for explicit navigation rules2. checks to whether the action outcome corresponds to a
view id. If yes navigate to view.
• Bean Validation Support• OOTB JSR 303 support (@NotNull, @Length, etc.)
• Error Handling• ExceptionHandler API for “global” or “central” exception
handling activities
Agenda
1. What is JSF and why JSF?2. Architecture Overview3. UI Component Model4. Summary of new features in JSF 25. Development Steps
Steps in Development Process
1. Develop model objects which hold the data2. Register model objects (managed bean) in
faces-config.xml or use annotations3. Create Pages using UI component and core
tags4. Define Page Navigation in faces-config.xml or
use implicit navigation5. Configure web.xml
Step 1: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, session, request
• JSF keeps the bean's data in sync with the UI• data binding by means of EL
Step 2:Register model objects@ManagedBean(name = "appPreferences")@RequestScopedpublic class AppPreferences {
@ManagedProperty("#{persistenceThemeManager}")private ThemeManager themeManager;
private String theme;
// getters and setterspublic List<SelectItem> getThemesSelectItems() …
public void saveTheme() {themeManager.saveTenantTheme(selectedTheme);
}}
Register this class as a request scoped managed bean
Action handler
Model’s data
Step 3: Create JSF Page
Binding UI to Managed Bean
<h:inputText value="#{userEditBean.firstName}" required="true" />
@ManagedBean(name = "userEditBean")@SessionScopedpublic class EditUserBean {
public String getFirstName() {…
}
public void setFirstName(String firstName) {…
}
editUser.xhtml
EditUserBean.java
Step 4: Define Page Navigation Rules
<navigation-rule><from-view-id>/wizard/item.jsp</from-view-id><navigation-case>
<from-outcome>showPriceRecs</from-outcome>
<to-view-id>/wizard/pricesResult.jsp</to-view-id></navigation-case><navigation-case>
<from-outcome>goBack</from-outcome><to-view-id>/wizard/start.jsp</to-view-id></navigation-case>
</navigation-rule>
<h:commandButton action="goBack" image="../images/back-arrow-small.png"/> <h:commandButton value="Lookup Prices" action="#{multiCategorySelectBackingBean.lookupPrices}"/>
public String lookupPrices() {...
return "showPriceRecs";}
Web Page
Backing Bean
faces-config.xml
Step 5: Configure (web.xml)• Add<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>*.jsf</url-pattern>
</servlet-mapping>
• May be not necessary with Servlets 3.0 compliant container
Questions?