ChatGPT解决这个技术问题 Extra ChatGPT

JSF vs Facelets vs JSP [duplicate]

This question already has answers here: What is the difference between JSF, Servlet and JSP? (16 answers) Closed 6 years ago.

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:

That JSF is a replacement for JSP; and 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!


M
Michael Borgwardt

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).


@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.
It should be noted that you can use Facelets in JSF 1.2 just as well, if you just add the appropriate libs.
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!
@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.
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!
佚名

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


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?
Y
Yster

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:

https://i.stack.imgur.com/FCaqL.png


D
Daniel

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