Facelets is an XML-based view technology for the JavaServer Faces framework. You don't need to use this tag on every single JSF question where Facelets is "coincidentally" used as view technology. Use this tag only and only if you have a specific question or problem with Facelets' own
Facelets
Facelets is an XML-based view technology for the JavaServer Faces framework. Designed specifically for JSF, Facelets is intended to be a simpler and more powerful alternative to JSP-based views. Initially a separate project, the technology was standardized as part of JSF 2.0 and Java-EE 6 and has deprecated JSP. Almost all JSF 2.0 targeted component libraries do not support JSP anymore, but only Facelets.
Hello World
Preparing: Depending on the server used, JSF/Facelets may already be built-in (GlassFish, JBoss AS, WebSphere, etc.), or not (Tomcat, Jetty, etc.). If not, then you'd need to download a JSF implementation to your choice and drop the necessary JAR files in /WEB-INF/lib
folder.
Controller: First register the FacesServlet
in /WEB-INF/web.xml
as follows:
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
View: Then create a Facelets file hello.xhtml
as follows:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelets</title>
</h:head>
<h:body>
<h:form>
<h:inputText value="#{sessionScope.who}" />
<h:commandButton value="Say Hello" />
</h:form>
<h:outputText rendered="#{sessionScope.who != null}"
value="Hello, #{sessionScope.who}" />
</h:body>
</html>
Start the server and open it by http://localhost:8080/contextname/hello.xhtml
.
Model: if you also want to create the model, then head to JSF tag wiki.
Documentation
Online resources
Frequently asked questions
- Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?
- When to use
<ui:include>
, tag files, composite components and/or custom components? - How to package Facelets templates, tag files, composites, etc in JAR file?
- Which XHTML files do I need to put in /WEB-INF and which not?
- Is it possible to use JSF+Facelets with HTML 4/5?