109

I can't seem to get a clear answer as to the concrete difference between Java Server Faces vs. so-called facelets. Can anyone give me a clear-as-day answer?!?

Also, I understand that either JSF or JSP can be used to create dynamic web pages, but two seemingly-conflicting details are confusing me. I've heard both of the following:

  1. That JSF is a replacement for JSP; and

  2. JSF and JSP form different parts of the View in Java's web-tier MVC paradigm

So which is it? Logic dictates it almost certainly can't be both!

Pam
  • 1,093
  • 2
  • 8
  • 4
  • 2
    You will find answer on: http://www.realdevelopers.com/blog/development/facelets-vs-jsp also look at this: http://www.ibm.com/developerworks/java/library/j-facelets/ – Harry Joy Jan 27 '11 at 11:31
  • 3
    Related: [What's the difference between JSF, Servlet and JSP?](http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp/2097732#2097732). – BalusC Jan 27 '11 at 11:49

4 Answers4

133
  • JSF is a standardized Java framework for web UIs based on an MVC pattern
  • JSPs are a (much older) standard for generating web pages from templates - these can be used as the View in a JSF application, but also separately from JSF.
  • Facelets are an alternative view technology based on pure XML templates (no scriptlets) which was introduced with Version 2 of the JSF standard. They can only be used in a JSF application.

In the light of that, let's take a look at your conflicting statements:

That JSF is a replacement for JSP; and

Not quite true, since JSF can use JSPs for its view (and had to, prior to JSF 2). However, JSF apps using Facelets can be seen as a replacement for JSP-based technologies.

JSF and JSP form different parts of the View in Java's web-tier MVC paradigm

Completely wrong - JSF covers the entire MVC pattern (though it can overlap with EJBs, since both are based on annotations that can be mixed in the same class).

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • Thanks Michael - by "scriptlets" do you mean Java-based components like backing beans? – Pam Jan 27 '11 at 11:41
  • 2
    @Pam: no, "scriptlets" means java code snippets embedded in a JSP. These have been frowned upon for a long time, with tag libraries and EL as a cleaner replacement. The Facelet standard eliminates them completely. Backing beans (or EJBs taking their role) are still very much a part of a Facelets-based JSF app. – Michael Borgwardt Jan 27 '11 at 11:49
  • 1
    It should be noted that you can use Facelets in JSF 1.2 just as well, if you just add the appropriate libs. – Joeri Hendrickx Jan 27 '11 at 12:41
  • 4
    Ahhhh..... so: FaceletServlet is the controller, the Facelet is the XHTML view, and the model is a bean. Three last questions: (1) I assume that, as with servlets, one must subclass the FaceletServlet and override the service(HttpRequest,HttpResponse) method, yes? (2) Do best practices dictate 1 FaceletServlet per web page? I can't imagine it any other way! And (3)Is the model a @ManagedBean (EJB), a JavaBean or can it be any POJO? Thanks! – Eugie Jan 27 '11 at 14:28
  • 3
    @Pam: 1+2) You typically never deal with servlets directly in a JSF app. Instead you have configuration in faces-config.xml as well as annotations on various classes, which control what the FacesServlet does. 3) @ManagedBean is a JSF-specific annotation. Since JEE 6, you can also use EJBs directly (which have different annotations), but both are nowadays POJOs based on the JavaBean standard. If you don't want to use annotations, you can declare the managed beans in faces-config.xml, but JSF needs to be told what they are *somewhere*. – Michael Borgwardt Jan 27 '11 at 14:44
  • 1
    Thanks Michael - so if you don't deal with servlets directly in JSF, where do you put the business logic (the control)? Does this go in the "backing bean"? And is there only one face-config.xml file per JSF application, or does each webpage (Facelet) get its own XML config? Thanks again... I swear I'm starting to get this! – Eugie Jan 27 '11 at 14:49
  • @Pam: The business logic would typically be in action methods in the backing beans, or in other classes called by those methods. The FacesServlet's role as controller is mostly confined to handling navigation between different views based on the outcomes of those action methods. I guess that the backing beans should really be considered controllers as well. I think the most common setup is to have the actual model class instance as one property within the backing bean, which the action methods can then access easily. – Michael Borgwardt Jan 27 '11 at 14:58
  • +1 for: though it can overlap with EJBs, since both are based on annotations that can be mixed in the same class Thanks! – Koray Tugay Mar 01 '13 at 23:13
  • You say Facelets can only be used in a JSF application. This stackoverflow answer says otherwise: http://stackoverflow.com/questions/11569097/facelets-without-jsf. More contradiction! – Ryan Jun 13 '14 at 15:23
  • @Ryan: that answer sounds like you then basically have to reimplement half of JSF in your servlet. Not exactly practical. – Michael Borgwardt Jun 13 '14 at 15:31
  • @"Michael Borgwardt" - So making a web application before JSF existed was impractical? No one could manage to parse some parameters, validate them, and call a business method before? I guess without that overly complex extra level of indirection, interaction with a pointless copy of the view DOM server side, and responding to requests via hooks into an unnecessarily expanded life-cycle no one could get by? – Ryan Jun 13 '14 at 16:12
  • 1
    @Ryan: People could get by without Facelets as well, what's your point? If you want a different web framework, you know where to look, If you want to cook your own, fine. It just seems silly to rip out the rather tightly integrated templating mechanism and then reimplement much of what you just got rid of, even if you leave out parts you don't like. I doubt it's quite as straightforward as BalusC makes it sound too. – Michael Borgwardt Jun 13 '14 at 20:48
  • Your older avatar was better. – Koray Tugay Oct 01 '14 at 13:50
10

From Wikipedia article(JSF)

JSF is a request-driven MVC web framework based on component driven UI design model, using XML files called view templates or Facelets views.

JSF is web framework and Facelet is a view template for JSF

From Wikipedia article(JSP)

JavaServer Pages (JSP) is a Java technology that helps software developers serve dynamically generated web pages based on HTML, XML,

Also look at this topic Difference between jsf and jsp

  • 1
    So, if I understand correctly: JSF consists of all the component tag libraries, converter classes, validator classes, etc., whereas the "facelet" is simply the XHTML file that uses those component tags and binds to the backing beans? – Pam Jan 27 '11 at 11:44
6

Java Server Pages (JSP) is java technology which enables Web developers and designers to rapidly develop and easily maintain, information-rich, dynamic Web pages that leverage existing business systems. JSP technology separates the user interface from content generation, enabling designers to change the overall page layout without altering the underlying dynamic content.

Facelets is the first non JSP page declaration language designed for JSF (Java Server Faces) which provided a simpler and more powerful programming model to JSF developers as compare to JSP. It resolves different issues occurs in JSP for web applications development.

Here is a table that compares the features of scriplets and facelets:

enter image description here Source

Yster
  • 3,147
  • 5
  • 32
  • 48
3

I've been looking for an answer for the same question. I found these links very helpful:

An Example of JSP application model: http://netbeans.org/kb/docs/javaee/ecommerce/design.html#architecture

Basic JSF Concepts, some by examples: http://netbeans.org/kb/docs/javaee/ecommerce/conclusion.html#framework

Daniel
  • 754
  • 1
  • 11
  • 25