Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

API Specification and Documentation


Tutorials



Intro

JSF (Java Server Faces)

What it is:
  • JavaServer Faces (JSF) is a Java web application framework intended to simplify development and integration of web-based user interfaces;
  • JSF is part of the Java Enterprise Edition;
  • JSF is a request-driven MVC web framework for constructing user interfaces using components;
  • As a display technology, JSF 2 uses Facelets: previous versions (JSF 1.x) used JavaServer Pages (JSP) instead of facelets for its display technology;
  • JSF is a standardized technology which was formalized in a specification through the Java Community Process. So, in order to code in JSF, you need to use an implementation of this specification (Mojarra is the reference implementation).

JSF is a specification for which there is a couple of implementations available:
  • Mojarra: is the JSF reference implementation, distributed by Oracle;
    It was previously named "Sun JSF Reference Implementation" (aka Sun JSF RI) until version 1.2_08;
    It's part of the Glassfish project;
     
  • MyFaces: The Apache Foundation JSF implementation with Ajax components;
References:

JSF Component libraries:
Don't confuse JSF implementations with component libraries!
There's a couple of component libraries that you can use with a JSF implementation (check this matrix for a comparison between JSF component libraries):
  • RichFaces: an open source Ajax-enabled component library for JavaServer Faces, hosted by JBoss. It allows easy integration of Ajax capabilities into enterprise application development.
  • IceFaces: open-source, Java JSF extension framework and rich components, Ajax without JavaScript;
  • many more;

Core JSF features
  • Managed Beans (aka "Backing Beans" or "Page Beans"): normal JavaBean classes annotated with @ManagedBean that will supply the faceletes views with properties (views access this properties through EL to gather information for display);
  • A template-based component system: you no longer need to use the include directives used on JSP+Servlets;
  • Built-in Ajax support: using <f:ajax /> (since JSF v2.0);
  • Built-in support for bookmarking & page-load actions;
  • Integration with the Unified Expression Language (EL), which is core to the function of JSF. Views may access managed bean fields and methods via EL: <my:component rendered="#{myBean.userLoggedIn}" />. This lets you separate presentation (facelets views) from busines logic (managed beans): facelets views will only contain declarative code (HTML; facelets components; EL placeholders) and use EL to call managed beans properties (managed beans are the only ones to contain Java code and responsible for all the logic);
  • A default set of HTML and web-application specific UI components:  and you can use many of-the-shelf component libraries;
  • A server-side event model : For dispatching events and attaching listeners to core system functionality, such as "Before Render Response" or "After Validation"
  • State management, supporting: "request", "session", "application", "flash", and "view" scoped Java beans.
  • Two XML-based tag libraries (core and html) for expressing a JavaServer Faces interface within a view template (can be used with both JSP or Facelets)

Facelets (aka: View templates or Facelets views)

Facelets are the XML files (xhtml) that define the JSF views. For this they are also called "view templates" or "Facelets views".
The term Facelets refers to the view declaration language for JSF technology. JSP was used as the presentation technology for JSF 1.x, but JSP does not support all the new features available in JSF 2.0. For this JSP technology is considered to be a deprecated presentation technology for JavaServer Faces 2.0. Facelets is a part of the JSF specification and also the preferred presentation technology for building JSF technology-based applications.
Facelets (which was designed specifically for JavaServer Faces) was adopted as the official view technology for JSF 2.0. This eliminates the life-cycle conflicts that existed with JSP, forcing workarounds by Java developers. Facelets allows easy component/tag creation using XML markup instead of Java code, the chief complaint against JSF 1.x.(Wikipedia also has good facelets info)

Facelets applications are a type of JSF applications that use XHTML pages rather than JSP pages.

Using facelets taglibs in XHTML:

Namespace declarationDescription
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="enDefault XHTML namespace
xmlns:ui="http://java.sun.com/jsf/facelets"Facelets UI tags like ui:compose and ui:define
xmlns:h="http://java.sun.com/jsf/html"JSF HTML tags
xmlns:f="http://java.sun.com/jsf/core"JSF core tags
xmlns:c="http://java.sun.com/jstl/core"JSTL core tags
NOTE: Avoid using JSTL tags with Facelets! Because JSTL doesn't work with the JSF view tree, this will cause unexpected results. Yes, you can use some of the JSTL core tags with Facelets. However, there are a few that are not supported because they are either redundant (there is a JSF equivalent that is preferred) or they simply don't fit in to the JSF way of doing things.

Components/Widgets/Controls

DropDownList

ref: Managed Beans I: Using Java Classes to Represent Form Info

Some usefull classes:

  • javax.faces.model.SelectItem: represents a single item in the list of supported items associated with a UISelectMany or UISelectOne component.
    Useful for example if you want the dropdownlist to have a value (like and id) and a label (some text) for each one.
    The constructor has many overloads, ex:
    SelectItem(Object value)         
    SelectItem(Object value, String label)         
    SelectItem(Object value, String label, String description, boolean disabled, boolean escape, boolean noSelectionOption)


Example:

Download source code here
  • Company class is just a POJO Javabean with no annotations;
  • DaoMock class is just a mockup with a harcoded Company[] and methods to access this elements
  • The xhtml form:
<h:form>           
    <legend>Select Company:<br />                 
        <h:selectOneMenu value="#{myObject.companyId}">
            <f:selectItems value="#{companyOptions.companyNames}"/>
        </h:selectOneMenu>
    </legend>
    <h:commandButton value="submeter" action="#{myObject.controller()}" />
</h:form> 

  • MyObject is the @ManagedBean of the form:
@ManagedBean
public class MyObject {

    private Company selectedCompany; //will be set by the controller method after the form is submitted
    private int companyId; //this is the value set by the dropdown list

    /**
     * Used to submit the form
     * @return the destination page
     */
    public String controller() {
        DaoMock dao = new DaoMock();

        this.selectedCompany = dao.getCompanyById(companyId);
        return "resultado";
        //TODO: if companyNotFound return String for error page instead
    }
//setters/getters omitted. NOTE: selectedCompany only has get, no set - it will be set by the controller method


  • CompanyOptions class is used to get the dropDownList items and is also a @ManagedBean
@ManagedBean
public class CompanyOptions {

    public List<SelectItem> getCompanyNames() {
        List<SelectItem> listOfItems = new ArrayList<>();
        DaoMock dao = new DaoMock();
        Company[] companyArray = dao.getCompanies();

        for (Company c : companyArray) {
            listOfItems.add(
                    new SelectItem(c.getId(),c.getNome()));
        }

        return listOfItems;
    } 

O
TODO

TODO

Sem comentários:

Enviar um comentário