I am following the Java EE6 tutorials and I don't understand the difference between JSF and Facelets. Chapter 4 goes has a section 'Developing a Simple JavaServerFaces Application' and Chapter 5 has a section 'Developing a simple Facelets' application and as far as I can see both these examples use the exact same process - an .xhmtl page for the markup, a backing bean to hold some data and then a web.xml file that maps urls to the 'FaceletsServlet'.
From what I can gather Facelets are .xhtml pages with tags such as <h:head> and <h:inputText> used to mark up the webpages.
But then what is JSF? I have read that either JSP or Facelets can be used with JSF so does that mean the role of JSF is to provide the tag libraries that are used by JSP or Facelets?
JSF - Java Server Faces is the overarching application development framework. It contains many aspects, one of which is the view handler. JSF (Wikipedia), JSF (Stack Overflow).
Facelets is a view handler for JSF. Before JSF 2.0, JSP was the default view handler, but the problem was JSP didn't work very well with JSF's component based framework. Facelets was designed from the ground up to work well with JSF. Facelets (Wikipedia), Facelets (Stack Overflow)
Tag libraries are a different part of the JSF spec. They are collections of UI components. Facelets is not a taglib, and many tag libs can be used with Facelets.
JSF is as you probably already gathered a component based system. These components are organized in libraries.
JSF is the base standard and defines the standard taglibs core and html. These include the tags for such basic elements as input fields, buttons, labels and links. Facelets is the facelets taglib, it was originally distributed seperatly but now it is included in the JSF standard. Facelets contains tags like ui:composition, ui:insert and ui:define that can be used for templating. There are many other tag libraries that can be used with JSF like primefaces, richfaces, tomahawk and trinidad.
JSF is used with Facelets, before we were using jsp. Facelet is a view technology based on xml.
Facelets was originally created as a separate, alternative view declaration language for JSF 1.1 and JSF 1.2 which both used JSP as the default view declaration language. Starting from JSF 2.0, Facelets has been promoted by the JSF expert group to be the default view declaration language. JSP has been deprecated.
Important features facelets provides are templating, composite components, content reuse.
http://www.ibm.com/developerworks/library/j-facelets/
Related
I started using JSF 2.0 with Facelets recently and got puzzled by new composite components knowing existing <ui:include> and other templating techniques offered by Facelets 1.x.
What is the difference between those approaches? Functionally they seem to offer about the same: <ui:param> vs <cc:attribute>, <ui:insert>+<ui:define> vs tag files, reuse of the existing templates. Is there anything besides syntax and clear interface specification in case of composite components? Could performance differ?
What is the difference between those approaches?
Facelet templates
Use Facelet templates (as in <ui:composition>, <ui:include> and <ui:decorate>) if you want to split main page layout fragments into reuseable templates. E.g. header, menu, content, footer, etc.
Examples:
How to include another XHTML in XHTML using JSF 2.0 Facelets?
What is the real conceptual difference between ui:decorate and ui:include?
How to customize h:head when using ui:composition template?
How to change head elements of a page when using ui:composition
How to ajax-refresh dynamic include content by navigation menu? (JSF SPA)
Facelet tag files
Use Facelet tag files if you want to have a reuseable group of components in order to prevent/minimize code duplication. E.g. a group of label+input+message components. The major difference with composite components is that the output of a Facelet tag file does not represent a single UIComponent and may in some circumstances be the only solution when a composite component doesn't suffice. Generally, having an <ui:include> with one or more <ui:param> which passes a managed bean property (and thus not a hardcoded value) is a signal that the include file can better be a tag file.
Examples:
How to create a custom Facelets tag?
How to make a grid of JSF composite component?
How to create a composite component for a datatable column?
Primefaces outputLabel for composite component
Composite components
Use composite components if you want to create a single and reuseable custom UIComponent with a single responsibility using pure XML. Such a composite component usually consists of a bunch of existing components and/or HTML and get physically rendered as single component and is supposed to be bound to a single bean property. E.g. a component which represents a single java.time.LocalDate property by 3 dependent <h:selectOneMenu> components representing day, month and year, or a component which combines <p:fileUpload> and <p:imageCropper> into a single <my:uploadAndCropImage> referring a single custom com.example.Image entity as property.
Examples:
Our Composite Component wiki page
The BalusC Code: Composite Component with multiple input fields
Split java.util.Date over two h:inputText fields representing hour and minute with f:convertDateTime
Select all items in Multiple SelectManyCheckBox with dynamic ids
Extending JSF commandLink component
Avoiding duplicate ids when reusing facelets compositions in the same naming container
Custom components
Use a custom component whenever the functionality cannot be achieved with Facelet tag files or composite components, because of the lack of support in the standard/available set of components. Generally when you want a high degree of control and/or customization of the decoding and/or encoding, and also to offer the endusers the possibility to relatively easily extend/override the decoding and/or encoding. Examples can be found over all place in source code of open source component libraries such as PrimeFaces and OmniFaces.
Tag handlers
When you want to control the building of the JSF component tree instead of rendering of the HTML output, then you should use a tag handler instead of a component.
Examples:
Custom Facelet component in JSF
How can I access the content of something created with <ui:define> programmatically?
Conditional render in tagfile depending on whether the attribute is specified or not
Performing a redirect, when conversion / validation associated with query parameters fails
Example projects
Here are some example projects which utilize all of above mentioned techniques.
Java EE Kickoff App (templates - includes - tagfiles - composite)
OmniFaces Showcase (templates - includes - tagfiles - composite)
Could performance differ?
Technically, the performance concern is negligible. The choice should be made based on the concrete functional requirements and the final degree of abstraction, reusability and maintainability of the implementation. Each approach has its own well definied purpose and limitations.
Composite components do however have a significant overhead during building/restoring of the view (specifically: during saving/restoring the view state). And, in older versions of Mojarra, composite components had performance issues with assigning default values, this is already fixed since 2.1.13. Also, Mojarra had a memory leak when a <cc:attribute method-signature> is used for method expressions, basically the entire component tree is re-referenced in HTTP session, this is fixed since 2.1.29 / 2.2.8. The memory leak can be bypassed in older 2.1 versions as below:
<context-param>
<param-name>com.sun.faces.serializeServerState</param-name>
<param-value>true</param-value>
</context-param>
Or in older 2.2 versions as below:
<context-param>
<param-name>javax.faces.SERIALIZE_SERVER_STATE</param-name>
<param-value>true</param-value>
</context-param>
Still, when you have relatively "a lot of" composite components, and you have javax.faces.STATE_SAVING_METHOD set to client, then the performance will be a pain. Do not abuse composite components if you merely want the basic functionality which is already possible with a simple include file or tag file. Do not use the ease of configuration (read: no *.taglib.xml file needed) as an excuse to prefer composite components over tag files.
When using Mojarra 2.2.10 or older, do not forget to disable the relatively short Facelets refresh period for production mode:
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>-1</param-value>
</context-param>
Do not use this setting for development, otherwise you've to restart the whole server to get changes in Facelets files to be reflected! Mojarra 2.2.11 and newer, and MyFaces already defaults to -1 when javax.faces.PROJECT_STAGE is not set to Development.
We are migrating JSF 1.1 (MyFaces) project to JSF 2. The idea is to migrate periodically by keeping both JSP and XHTML together for some time. We use many ajax4jsf-1.1.1 tags in JSP pages. We don't use RichFaces. After configuring the system to JSF 2 (with all config changes mentioned in tutorial by Balusc) When tried to access the JSP page with ajax4jsf.jar in classpath, we get an exception:
Caused by: java.lang.IllegalStateException: setViewHandler may not be executed after a lifecycle request has been completed
at org.apache.myfaces.application.ApplicationImpl.setViewHandler(ApplicationImpl.java:853)
at org.ajax4jsf.framework.ajax.InitPhaseListener.beforePhase(InitPhaseListener.java:92)
at org.apache.myfaces.lifecycle.PhaseListenerManager.informPhaseListenersBefore(PhaseListenerManager.java:76)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:131)
It looks ajax4jsf.jar is not compatible with JSF 2. Looks some issue with LifeCycle configuration.
Is there any way we can make a4j work with JSF 2 JSPs? I know when we use XHTML we don't need all this.
Get rid of Ajax4jsf 1.x altogether. It's indeed not compatible with JSF2. Instead, JSF2 offers a new main ajax tag <f:ajax> which covers all the core functionality as previously offered by Ajax4jsf 1.x.
If upgrading to RichFaces 4 is not an option (because, as you said yourself, you aren't using RichFaces components anywhere), then just remove Ajax4jsf 1.x and replace all <a4j:xxx> tags by standard JSF2 equivalents.
<a4j:ajaxListener>: use <f:ajax listener>.
<a4j:keepAlive>: just put managed bean in the view scope by #ViewScoped.
<a4j:log>: use jsf.ajax.addOnEvent() or jsf.ajax.addOnError() in JS context.
<a4j:commandLink>: just nest <f:ajax> inside <h:commandLink>.
<a4j:outputPanel>: use <h:panelGroup> and remember to include its ID in <f:ajax render> or PrimeFaces <p:outputPanel>.
<a4j:repeat>: just use standard <ui:repeat>.
<a4j:form>: just use <h:form>, it will autorecognize <f:ajax>.
<a4j:htmlCommandLink>: just nest <f:ajax> inside <h:commandLink>.
<a4j:jsFunction>: just use standard <h:commandScript>. It was however introduced late in JSF 2.3. If you can't upgrade to JSF 2.3 then consider OmniFaces <o:commandScript> or PrimeFaces <p:remoteCommand>.
<a4j:region>: just use <f:ajax execute>, you can even wrap <f:ajax> around a group of components.
<a4j:loadBundle>: just use standard <f:loadBundle>.
<a4j:status>: use jsf.ajax.addOnEvent() or jsf.ajax.addOnError() in JS context.
<a4j:actionparam>: just use standard <f:param>.
<a4j:loadScript>: just use standard <h:outputScript>.
<a4j:mediaOutput>: no replacement. Consider PrimeFaces <p:media>.
<a4j:poll>: no replacement. Consider OmniFaces <o:commandScript> or PrimeFaces <p:poll>.
<a4j:commandButton>: just nest <f:ajax> inside <h:commandButton>.
<a4j:include>: just use standard <ui:include>.
<a4j:loadStyle>: just use standard <h:outputStylesheet>.
<a4j:support>: just use standard <f:ajax>.
You also need to rename/rewrite JSP files to Facelets files. In simple cases, this is usually just a matter of changing root declarations and file extensions. Facelets makes it easier to replace all duplicated code by a single template. The following answer applies:
Migrating from JSF 1.2 to JSF 2.0
Is there any way of iterating over a list in JSF 1.2 without using any third party components? I know I can do it using Tomahawk. I also know that it can be done using JSTL, but I am keeping that as my last resort. Also I cannot use <ui:repeat> since we are using JSF 1.2. Is there any elegant way like <ui:repeat> to do it in jsf 1.2?
The only JSF 1.2 component which can iterate over a List is the <h:dataTable>.
In JSP, the only other "standard" (i.e. not "3rd party") tag which can iterate over a List is the JSTL <c:forEach>. Using JSTL shouldn't harm that much if the List which you'd like to iterate over is already available during view build time. You'll only run into trouble when it's only available during view render time, for example because it's been nested in a <h:dataTable> and should be iterating over a property of table's var. This just won't work due to reasons also mentioned in JSTL in JSF2 Facelets... makes sense?
There are no other ways without using a 3rd party library such as Tomahawk's <t:dataList>, unless you're open to reinventing the wheel by creating a custom UIComponent yourself. This is however not a trivial job.
It's however possible to integrate Facelets 1.x in JSF 1.2. A guide is described in the Facelets 1.x docbook. This is only going to be quite some of work if you already have an existing JSF application using JSP as view technology; you'd need to convert JSP to Facelets. But it'll in end make the upgrade path to JSF 2.x so much easier. See also a.o. Migrating from JSF 1.2 to JSF 2.0 and Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
we are going to migrate our JSF 1.2 project to JSF 2. We are already using facelets (not JSPs) as our presentation technology.
We are using lot of JSTL -> "xmlns:c="http://java.sun.com/jstl/core"
We are using lot of <c:if> statements. I know that JSF2 brings rendered attribute, which should also evaluate boolean expressions.
Should I bother rewriting stuff to new rendered attribute or is it ok to continue using JSTL in JSF2?
JSTL works the same way in JSF2 as it worked in JSF1. The rendered attribute is not JSF2 specific, it has been in JSF all the lifetime long. If those JSTL tags have always worked as intended in your JSF 1.2 application, then you don't necessarily need to migrate them for JSF2. You only need to change the taglib uri to include the /jsp (!!) path.
xmlns:c="http://java.sun.com/jsp/jstl/core"
However, whenever possible, using the rendered attribute definitely is more recommended than relying on JSTL tags. For sure if you plan to bind JSTL tag attributes to a #ViewScoped bean. See also Communication in JSF 2.0 - #ViewScoped fails in taghandlers.
Is it possible to use both composite component facelet (xhtml, cc.attrs and etc. ) and component class ( extended from UIComponent) in one component?
You cannot use Facelets composite components in JSF UI components, but you can use (custom) JSF UI components in Facelets composite components.
Composite components are a Facelets feature, not a JSF feature. The relation between JSF and Facelets is that Facelets is a view technology which is designed with JSF in mind. It's in essence the successor of JSP and by default included/supported since JSF 2.0.