Introduction to JavaServer Pages (Part 2)
Facelets Declaration Language
In part 1 I introduced the basic idea behind JavaServer Pages (JSF). In this article, I would like to introduce the Facelets declaration language.
The first tags we meet are the HTML tags that represent HTML elements. These are really just syntactic sugar over HTML tags such as input, radio button etc. but provide some cross-browser compatibility. You are not required to use them; so you can just use the normal HTML tags if you prefer.
<h:form>, <h:inputText>, <h:commandButton>, <h:selectOneRadio>, <h:inputTextArea>, <h:outputText>, <h:commandLink> ...
An important feature of the Facelets language is the ability to create templates for reusability and repeatability. We are provided with a selection of tags that allow this to be done in quite a logical fashion, such as the repeat tag that repeats a section code and the define tag that’s start a components definition.
<ui:composition>, <ui:insert>, <ui:repeat>, <ui:fragment>, <ui:debug>, <ui:decorate>, <ui:param>, <ui:define> ...
At the heart of Facelets tags are the core tags. These add more functionality through converters, action listeners, validators and much more. This is where you will find functionality such as AJAX, language resource bundles and so on.
<f:convert>, <f:convertDateTime>, <f:lengthvalidate>, <f:ajax>, <f:loadBundle>, <f:selectItem>, <f:actionListener> …
And now we come to the concept of a scope. Beans are defined as having a scope. The scope of a bean determines its lifecycle. The scope also determines which clients refer to which instances of the bean. By default, beans are given the scope of dependent.
An instance of a dependent bean is never shared between different clients. It is instantiated when the object it belongs to is created and destroyed when the object it belongs to is destroyed.
New Java EE 7 scopes
In Java EE 7 some new bean scopes were introduced namely @ViewScoped, @FlowScoped and @FlowDefinition.
Briefly, ViewScoped beans share the same lifecycle as the view which initially referenced them. This is particularly useful for views that make use of AJAX. FlowScoped and FlowDefinition are used with Faces Flow which connects logically related pages together and allows the bean’s lifecycle to be defined for that collection of views.
If you want to learn more about JSF and Java EE why now take my course Learning Java Enterprise Edition. In this course, I teach you the basics of a range of Java EE APIs. If you are really serious and want to give your career a boost the following courses are just what you are looking for:
- learn to develop an online bookshop with JAX-RS,
- learn WebSocket Programming in Java EE and
- become a JSON-Processing master.
Want to learn more about Java EE then these articles should interest you:
- JAX-RS API: an API for the construction of RESTful services
- Design Patterns: Professional Java EE design patterns
- JavaServer Faces: The JSF view language
Leave a Reply