I see that from JSF2.0 onwards Facelets view definition language is the preferred view definition language and not JSP which has been deprecated as a legacy fall back. I want to understand why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards ? I know that JSP also has some templating behavior which is the main driving point for adopting Facelets .
P.S: I have been through this post on stackoverflow but I do not think it answers my question. Hence posting this as a separate question .
True, JSP has some templating capabilities, but the biggest disadvantage of using JSP in JSF is that JSP writes to the response as soon as it encounters template text content, while JSF would like to do some pre/post processing with it. In JSF 1.0/1.1 the following JSF code
<h:outputText value="first"> second <h:outputText value="third"> fourth
would produce
second fourth first third
This was during the JSF 1.0/1.1 ages a headache. Developers would need to wrap template text like second
and fourth
in the above example in <f:verbatim>
tags over all place. JSF 1.2 has solved it with an improved view handler which parses the JSP instead of executing it, but it was under the hoods still very clumsy as JSP syntax isn't "well-formed" like XML. A XML based view technology was strongly desired, so that an efficient SAX based parser could be used. And Facelets was born (among Ken Paulsen's "JSFTemplating").
Also, unified EL #{}
couldn't be used in JSP template text, resulting in ugly —and for starters unintuitive— mixing of ${}
and #{}
. Also, JSTL could in JSF 1.x on JSP not be used as view build time tags. Also, JSP syntax with <% %>
things is old school and the possibility of embedding raw Java code in JSP is considered a very poor practice which breaks MVC ideology.
All with all, in JSF/MVC perspective, JSP is simply ugly and terrible and Facelets is simply clean and awesome.
I found the following answers on the Internet.
JSFToolbox documentation chapter 3:
JSP Compile-Time Overhead
Every time you edit, save and reload a JSP page, the server's JSP compiler generates Java servlet code and compiles it into a servlet. This is called the JSP translation process, and typically it costs between 1-2 seconds, depending on server performance.
Facelets XML Compilation
Unlike JavaServer Pages, Facelets pages are not compiled into servlets. Since Facelets pages are XML-compliant, the Facelets framework uses a fast SAX-based compiler to build your views. Also, Facelets can be configured to detect and render changes to your pages immediately, speeding up your JSF development cycle.
Book "JSF 1.2 components" from Ian Hlavats, page 49:
During JSF application development, we often make changes to our JSF pages, resulting in frequent recompilation of our JSP pages, and this compile-time overhead can add up.
Facelets pages are simple XML documents (XHTMl pages) that are never compiled to servlets rather it uses the SAX-based compilation process that constructs the UI component tree for our views. Hence Facelets are faster compared to JSP since it is free from the JSP translation overhead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With