Data Loading...
Java Server Faces (JSF)
Java Server Faces (JSF)
JSF is used for building Java Web application interfaces. Like Swing and AWT, JSF is a development framework that provides a set of standard, reusable GUI components. JSF provides the following development advantages:
Clean separation of behavior and presentation Component-level control over statefulness Events easily tied to server-side code Leverages familiar UI-component and Web-tier concepts Offers multiple, standardized vendor implementations JSF's fine-tuned event model allows your applications to be less tied to HTTP details and simplifies your development effort.
Java Server Faces (JSF)
A typical JSF application consists of the following parts:
JavaBeans components for managing application state and behavior
Event-driven development (via listeners as in traditional GUI development)
Pages that represent MVC-style views; pages reference view roots via the JSF component tree
JSF MVC Design
The model-view-controller (MVC) architecture provides a set of design patterns that help you separate the areas of concern involved in building and running a GUI-based application:
The model encapsulates the business logic and persistence code for the application. The model should be as view-technology-agnostic as possible. For example, the same model should be usable with a Swing application, a Struts application, or a JSF application. The view should display model objects and contain presentation logic only. There should be no business logic or controller logic in the view. The controller (along with its attending logic) acts as the mediator between the view and the model. The controller talks to the model and delivers model objects to the view to display. In an MVC architecture the controller always selects the next view.
JSF MVC Implementation
In JSF's MVC implementation, backing beans mediate between the view and the model. Because of this, it's important to limit the business logic and persistence logic in the backing beans. One common alternative is to delegate business logic to a façade that acts as the model. Unlike JSP technology, JSF's view implementation is a stateful component model. The JSF view is comprised of two pieces: the view root and JSP pages.
The view root is a collection of UI components that maintain the state of the UI The JSP page binds UI components to JSP pages and allow you to bind field components to properties of backing beans
JSF MVC Implementation
JSF Example
JSF Example
To build the Calculator application in JSF you'll need to do the following:
1.
Declare the Faces Servlet, and Faces Servlet mapping in the web.xml file. Declare what beans get managed by JSF in the faces-config.xml file. Declare the navigation rules in the faces-config.xml file. Develop the model object Calculator. Use the CalculatorController to talk to the Calculator model. Create the index.jsp page. Create the calculator.jsp page. Create the results.jsp page.
2. 3. 4. 5. 6. 7. 8.
Declare the Faces Servlet and Servlet mapping
Faces Servlet /calc/*
The above tells the Faces Servlet container to send all requests that map to /calc/ to the Faces Servlet for processing.
Declare bean management Next, you will want to declare which beans get used by JSF GUI components. The example application only has one managed bean. It is configured in faces-config.xml as follows:
...
The "backing file" bean that backs up the calculator webapp CalcBean com.arcmind.jsfquickstart.controller.CalculatorController session
The above config tells JSF that you want to add a bean to the JSF context called CalcBean. You can call your managed bean anything you want.
Declare navigation rules
For this simple application you need only to establish the navigation path from the calculator.jsp page to the results.jsp page, as shown below.
/calculator.jsp
success /results.jsp
Develop the model object package com.arcmind.jsfquickstart.model; public class Calculator { //add numbers. public int add(int a, int b) { return a + b;
} // multiply numbers public int multiply(int a, int b) { return a * b;
}
} With that, the business logic is all set up. Your next step is to glue it to the Web application interface.
Gluing the model and the view through the controller package com.arcmind.jsfquickstart.controller; import com.arcmind.jsfquickstart.model.Calculator; public class CalculatorController { private Calculator calculator = new Calculator(); private int firstNumber = 0; private int result = 0; private int secondNumber = 0; public CalculatorController() {super(); } public void setCalculator(Calculator aCalculator) { this.calculator = aCalculator; } public void setFirstNumber(int aFirstNumber) { this.firstNumber = aFirstNumber; } public int getFirstNumber() { return firstNumber; } public int getResult() { return result; } public void setSecondNumber(int aSecondNumber) { this.secondNumber = aSecondNumber; } public int getSecondNumber() { return secondNumber; } public String add() { result = calculator.add(firstNumber, secondNumber); return "success"; } public String multiply() { result = calculator.multiply(firstNumber, secondNumber); return "success"; } }
Gluing the model and the view through the controller package com.arcmind.jsfquickstart.controller; import com.arcmind.jsfquickstart.model.Calculator; public class CalculatorController { private Calculator calculator = new Calculator(); private int firstNumber = 0; private int result = 0; private int secondNumber = 0; public CalculatorController() {super(); } public void setCalculator(Calculator aCalculator) { this.calculator = aCalculator; } public void setFirstNumber(int aFirstNumber) { this.firstNumber = aFirstNumber; } public int getFirstNumber() { return firstNumber; } public int getResult() { return result; } public void setSecondNumber(int aSecondNumber) { this.secondNumber = aSecondNumber; } public int getSecondNumber() { return secondNumber; } public String add() { result = calculator.add(firstNumber, secondNumber); return "success"; } public String multiply() { result = calculator.multiply(firstNumber, secondNumber); return "success"; } }
Notice that the multiply and add methods return "success." The string success signifies a logical outcome. Note that it is not a keyword. You used the string success when specifying navigation rules in faces-config.xml; therefore, after the add or multiply operation is executed the application will forward the user to the results.jsp page. With that, you're done with the backing code. Next you'll specify the JSP pages and component trees that represent the application view.
Create the index.jsp page
The purpose of the index.jsp page in this application is to ensure that the /calculator.jsp page loads in the JSF context so that the page can find the corresponding view root. The index.jsp page looks as follows:
All this page does is redirect the user to calculator.jsp under the "calc" Web context. This puts the calculator.jsp page under the JSF context, where it can find its view root.
Create the calculator.jsp page
Create the results.jsp page
First Number:
Second Number:
Result:
This results.jsp file is a relatively simplistic page that displays the addition results to the user. It accomplishes this through the tag. The tag takes an id and value attribute. The value attribute outputs the bean value as a string when rendered. The value attribute uses JSF to bind the output value to your backing bean properties (namely, firstNumber, secondNumber, and result).
Demo with NetBeans 5.5 and the Visual Web Pack Read Visual Web Pack documentation for details with more emphasis of the following: - Layout: Property Sheets, JSF Fragments - Data Binding: CachedRowSet, CachedRowSetDataProvider, ObjectListDataProvider
JSF Request Processing Lifecycle Response Complete
Request
Restore Component Tree
Apply Request Value
Process Events
Response Complete
Process Validations
Process Events
Render Response Response Complete Response
Render Response
Process Events
Invoke Application
Response Complete Process Events
Update Model Values
Conversion Errors
Validation or Conversion Errors
JSF - Request Processing Lifecycle
Restore component tree
Apply Request Values
The purpose of the apply request values phase is for each component to retrieve its current state. Component values are typically retrieved from the request parameters.
Process Validations
The controller examines the request and extracts the view ID, which is determined by the name of the JSP page. If the view doesn't already exist, the JSF controller creates it. If the view already exists, the JSF controller uses it. The view contains all the GUI components.
At this stage, each component will have its values validated against the application's validation rules.
Update Model
updates the actual values of the server-side model -- namely, by updating the properties of your backing beans.
JSF - Request Processing Lifecycle
Invoke Application
The
JSF controller invokes the application to handle Form submissions. The component values will have been converted, validated, and applied to the model objects, so you can now use them to execute the application's business logic.
Render Response
you
display the view with all of its components in their current state.
Render the page and send it back to client
JSF Component Event Handling
Model binds
Id Local Value Attribute Map
has
has UIComponent has
Child UIComponent
Render has
has has
Converters
Validators
JSF Standard UI Components
UIInput UIOutput UISelectBoolean UISelectItem UISelectMany UISelectOne UISelectMany UIGraphic
UICommand UIForm UIColumn UIData UIPanel
JSF HTML Tag Library
JSF Core Tag Library
Validator,
Event Listeners, and Converters
JSF Standard Library
Express
UI components in JSP
JSF HTML Tag Library
JSF HTML Tag Library
| Food Selection: |
| |
JSF Rendering Model Two Rendering Models (direct or delegated) Direct
Decoding Encoding
Delegated
Component
Renderer
Pluggable look and feel
JSF Rendering Model Render kit consists of a set of renders JSF reference implement must provide a render kit for all the standard UI components to generate HTML 4.01 Custom render kit can be provided to render UI components into a specific markup language
JSF – Managed Bean Use to separate presentation from business logic Based on JavaBeans Use the declarative model Entry point into the model and event handlers
JSF – Value Binding Bind
component value and attribute to model objects Literal:
Value Binding:
JSF – Value Binding
Value binding expression Bean
properties
List Array Map Predefine
objects- header, header values, request parameters, cookie, request/session/application scope attributes, initial parameters
JSF – Value Binding Expression
#{!user.manager} – operator (+, -, *, /, % …) Faces-config.xml
user org.User session
Support bean property initialization for primitive data type as well as List and Map. Binding expression can be used at the bean configuration at well
JSF – Predefined Objects Variable
Meaning
header
A Map of HTTP headers, only the first value of for each name
headerValues
A Map of HTTP headers, String[] of all values for each name
param
A Map of HTTP request parameters, first value of for each name
paramValues
A Map of HTTP headers, String[] of all values for each name
cookie
A Map of cookie name and values
initParam
A Map of initialization parameters
requestScope
A Map of all request scope attributes
sessionScope
A Map of all session scope attributes
applicationScope
A Map of all request scope attributes
facesContext
FacesContext instance of this request
view
The UIViewRoot instance of this request
JSF – Method Binding
Binding an event handler to a method
Four component attributes: Action Action
listener Value change listener Validator
JSF Events Events are fired by each UI component Event handlers are registered with each component
JSF Events – Value Change Event Value Changed Listener:
public void checkMaxUser(ValueChangeEvent evt) { evt.getNewValue(); // new value evt.getOldValue(); // old value }
JSF Events – Action Event Action Listener: