Now I Remember! Hello Worldwide Web: There are three things Your First JSF in I always forget. JDeveloper Names, faces, and— the third I can’t remember. Peter Koletzke Technical Director & Principal Instructor —Italo Svevo (1861–1928)
2
Survey Agenda • Job responsibilities? – DBA, developer • What is JSF? • Languages? Note: Examples use JDeveloper 11g and –PL/SQL • Related files ADF Faces. – Java – JSF –C++ • Accessing the database – Other Warning: This material contains • Tools? more depth than you need to – Developer Forms/Reports create apps in JDev 11g. – JDeveloper Slides will be on the NYOUG website soon. Look in the tech – Other journal for the white paper
3 4 JSF Application JSF Code • JavaServer Faces technology • Usually used for a web-based application • Relatively new technology – Technically can be any client – PDA, cell phone – Ratified JCP in 5/2004 • XML-like (or just plain old XML) – Part of Java EE (v.5) – Start and end tag required – Offers reference implementation – code library – Elements, commonly called “components” • Effort to simplify JSP development – Attributes – Component-ize it • High-level components provide much functionality • Its components represent JSP action tags – Integrate the controller – Requires a prefix and tag library definition (.tld file) • No Struts needed to back it – Write less HTML (than JSP) – Tag library definition points to the Java class that • Component handles HTML writing implements the tag • JSF is often run in a JSP file • Following example mixes standard JSF – XML-like tags: elements and attributes with ADF Faces Rich Client
5 6
JSF Code Snippet
7 Demo 1 8 JSF Communication Process The Steps Web Server 1. The browser issues an HTTP request to the web server. Which 2. The web server determines the application and passes Web Browser application? 1 the request to the web container (WLS or OC4J) TP HT st que 2 3 3. The web server reads web.xml to determine that this is a login Re JSF JSP. 4. The JSP is translated to a servlet and compiled (the first Web Container time it is run) 5. The web server passes the request to the Faces Servlet. login.jsp web.xml The Faces Servlet instantiates a life cycle class, which 7 JSP 4 processes the JSP JSF. HTTP translation re & compilation spo *** 6. The servlet accesses any necessary Model layer code to nse login.class 6 obtain values from the data source (such as a database). Database 7. The Faces Servlet then assembles the page and sends Faces 5 Note: JSP translation and it using an HTTP response to the browser. compilation is a one-time process Servlet
9 10
ADF Faces JSF Features • Oracle JSF component libraries • Rich component set – Released to MyFaces open source project in Jan. 2006 – Core library – for application tasks • Trinidad project at myfaces.apache.org – HTML library – for HTML tags, forms – Available in JDeveloper 10.1.3 as ADF Faces – JSP tag library included – In JDeveloper 11g as ADF Faces Rich Client • Can be implemented in other languages • Implements components available in UIX – Include data binding properties – Uses JSF mechanisms – Adds even more functionality to JSF • Embedded controller – Over 150 components, For example, selectOrderShuttle: – Previously, no standard controller for JSPs – Struts was/is a popular controller framework • Event-driven – Events on the component level – Like Forms triggers
11 12 Agenda JSF Files
Request • What is JSF? Web Tier Container Faces Servlet Other Model Code Life Cycle Backing bean Database • Related files 1 2 3 Response 6 5 4 • Accessing the database login.jsp login.class faces-config.xml
Message bundle HTML Render Kit Not to scale
web.xml
13 14
The Files web.xml • web.xml – used to start FacesServlet, which • web.xml –web module deployment instantiates and runs the life cycle class • faces-config.xml – the controller file used to descriptor manage page flow – Contains an entry such as this: • Backing bean – code for the components on the
Demo 2 15 16 web.xml Snippet faces-config.xml
17 18
Code View of Navigation Rules Navigation Case Outcome
• In addition to the “to” page, a navigation
19 20 Editing faces-config.xml Editing in the Overview Tab • JSF Navigation Diagram – Look under WEB-INF – Double click the faces-config.xml file in the navigator – Use drag and drop to add elements
21 22
Editing in the Structure Window Backing Beans • Backing bean: a Java class file used for code pertaining to a specific page – For example, login.jsp would use a Login.java backing bean • Contain accessors for components on the page and other code for just that page • Optional file: only create it if you need to change the logic • These are registered in faces-config.xml:
23 24 Creating Backing Bean Contents the Bean • Variables for each component • Create a Java class • Setters and Getters for each component from the New Gallery • If JDeveloper created the backing bean – Enter it in faces-config it will maintain it manually – Uses the comment line shown earlier in the • OR from the faces-config.xml file Create JSF Page – Adding a component adds the variable dialog and accessors for that component – Specify the name in – Deleting a component removes them faces-config and the – Renaming a component renames class file name them
25 26
Alternative for Creating the Managed Beans or Backing Beans? Backing Bean • Double click an action component •A bean (JavaBean) is a Java class file (button or link) with a standard set of methods – These dialogs will set up the Java class and register it in faces-config • Managed bean is a Java class file used to handle operations and data for a resource – such as a JSF page • Backing bean is a managed bean that supports a specific page • The terms “managed bean” and “backing bean” are sometimes used interchangeably
27 28 Backing Bean Snippet About Scope package login.view.backing; • Values in a bean or a FacesContext variable are import java.util.ResourceBundle; cleared out of memory at certain times import javax.faces.application.FacesMessage; import javax.faces.application.FacesMessage; • You can declare a scope for these objects: import javax.faces.context.FacesContext; – request: for the HTTP request/response // more imports – session: for the user’s session with the app server public class Login (until they log out or time out) { private RichForm loginForm; – application: across client sessions; held in the app private RichDocument d2; server memory private RichPanelHeader ph1; private RichPanelFormLayout pfl1; • ADF Controller offers additional scopes private RichInputText usernameField; private RichInputText passwordField; – pageFlow private RichCommandButton loginButton; private RichOutputText ot1; – view int loginAttempts = 0; – backingBean • Imports and private variables for each component
29 30
Backing Bean Snippet (continued) public void setLoginForm(RichForm form) Message Bundles { this.loginForm = form; } • Also called “resource bundles” public RichForm getLoginForm() { • Separate properties (text) or Java class return loginForm; } file containing labels and messages public void setUsernameField(RichInputText it1) { this.usernameField = it1; • Linked to the page through expressions } on the components public RichInputText getUsernameField() { return usernameField; • Also readable by code in the backing } bean // and accessors for all other components } • Allow for centralization of messages • Getters and setters for each component • Automated localization and • Other contents: validation code internationalization (language-specifics)
31 32 Message Bundles in JDeveloper Message Bundle Snippet • Define the message bundle name #English welcome message – Project properties – Resource bundle page WELCOME_HOME=Welcome Home • Add the message using a dialog # login attempt messages – Select “Select Text Resource” from the pulldown by incorrectLogin=Incorrect login. Try again. an applicable component loginHint=You seem to have forgotten the password. • Refer to the message using Expression Language, for example: #{viewcontrollerBundle.WELCOME_HOME} • This expression will be resolved at runtime and at design time Demo 3
33 34
Agenda The JDeveloper Technique • Create ADF Business Components • What is JSF? (ADF BC) – Declares database structures (tables and • Related files views) • Accessing the database – Similar functionality to EJBs • Drop data controls onto the page – This binds the components to the ADF BC components – A separate PageDef file is created for the bindings
35 36 Please fill out the evals Books co-authored with Dr. Paul Dorsey, Avrom Roy-Faderman, & Duncan Mills Summary Developer Designer Advanced Personal web site: • JSF evolved to make web development Handbook Forms & Reports http://ourworld.compuserve.com/ easier homepages/Peter_Koletzke • Some awareness of the runtime environment and life cycle will help in your first JSF JDeveloper 3 • You need to create the JSF JSP file Handbook ORACLE9i JDeveloper http://www.quovera.com • You also need supporting files: Handbook • Founded in 1995 as Millennia Vision – web.xml – created automatically to assist in Corp. loading pages ORACLE • Profitable for 7+ years without outside funding – faces-config.xml – the main JSF configuration file JDeveloper 10g • Consultants each have 10+ years – Backing beans – programmatic code for the page Handbook industry experience – Message bundles – centralized text strings • Strong High-Tech industry background for the page • 200+ clients/300+ projects • JDeveloper Partner • JDeveloper offers many tools to assist • More technical white papers and – Including frameworks to access the database presentations on the web site
What a surprise! 37 38