Why Facelets is preferred over JSP as the view definition language from JSF 2.0 onwards?
Asked Answered
J

2

36

I see that from JSF 2.0 onwards Facelets view definition language is the preferred view definition language and not JSP which has been deprecated as a legacy fallback. I want to understand why Facelets is preferred over JSP as the view definition language from JSF 2.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 Stack Overflow but I do not think it answers my question. Hence posting this as a separate question.

Jellaba answered 26/10, 2012 at 18:10 Comment(1)
tugay.biz/2016/07/sorry-what-is-facelets.htmlHordein
C
45

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 (as well as Ken Paulsen's "JSFTemplating" but ultimately Facelets was chosen as the new standard view technology for JSF 2.0).

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.

Constitutionalism answered 26/10, 2012 at 19:22 Comment(2)
@Constitutionalism "unified EL #{} couldn't be used in JSP template text". JSP2.1 does support unified EL.Isn't it ?Jellaba
Only in JSP tags. Not in template text. E.g. <p>#{user.name}</p> doesn't work in JSP. The ${} form does.Constitutionalism
S
12

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.

Skimp answered 16/10, 2014 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.