I have some Facelets files like below.
WebContent |-- index.xhtml |-- register.xhtml |-- templates | |--userForm.xhtml | `--banner.xhtml :
There are three main causes.
FacesServlet
is not invoked.FacesServlet
mappingThe URL of the link (the URL as you see in browser's address bar) has to match the
of the FacesServlet
as definied in web.xml
in order to get all the JSF works to run. The FacesServlet
is the one responsible for parsing the XHTML file, collecting submitted form values, performing conversion/validation, updating models, invoking actions and generating HTML output. If you don't invoke the FacesServlet
by URL, then all you would get (and see via rightclick, View Source in browser) is indeed the raw XHTML source code.
If the
is for example *.jsf
, then the link should point to /register.jsf
and not /register.xhtml
. If it's for example /faces/*
, like you have, then the link should point to /faces/register.xhtml
and not /register.xhtml
. One way to avoid this confusion is to just change the
from /faces/*
to *.xhtml
. The below is thus the ideal mapping:
facesServlet
javax.faces.webapp.FacesServlet
facesServlet
*.xhtml
If you can't change the
to *.xhtml
for some reason, then you probably would also like to prevent endusers from directly accessing XHTML source code files by URL. In that case you can add a
on the
of *.xhtml
with an empty
in web.xml
which prevents that:
Restrict direct access to XHTML files
XHTML files
*.xhtml
JSF 2.3 which was introduced April 2017 has already solved all of above by automatically registering the FacesServlet
on an URL pattern of *.xhtml
during webapp's startup. The alternative is thus to simply upgrade to latest available JSF version which should be JSF 2.3 or higher. But ideally you should still explicitly register the FacesServlet
on only one URL pattern of *.xhtml
because having multiple possible URLs for exactly the same resource like /register.xhtml
, /register.jsf
, /register.faces
and /faces/register.xhtml
is bad for SEO.
Since introduction of JSF 2.2, another probable cause is that XML namespaces don't match the JSF version. The xmlns.jcp.org
like below is new since JSF 2.2 and does not work in older JSF versions. The symptoms are almost the same as if the FacesServlet
is not invoked.
If you can't upgrade to JSF 2.2 or higher, then you need to use the old java.sun.com
XML namespaces instead:
But ideally you should always use the latest version where available.
One more probable cause is that multiple JSF implementations have been loaded by your webapp, conflicting and corrupting each other. For example, when your webapp's runtime classpath is polluted with multiple different versioned JSF libraries, or in the specific Mojarra 2.x + Tomcat 8.x combination, when there's an unnecessary ConfigureListener
entry in webapp's web.xml
causing it to be loaded twice.
com.sun.faces.config.ConfigureListener
When using Maven, make absolutely sure that you declare the dependencies the right way and that you understand dependency scopes. Importantingly, do not bundle dependencies in webapp when those are already provided by the target server.
JSF has a very steep learning curve for those unfamiliar with basic HTTP, HTML and Servlets. There are a lot of low quality resources on the Internet. Please ignore code snippet scraping sites maintained by amateurs with primary focus on advertisement income instead of on teaching, such as roseindia, tutorialspoint, javabeat, etc. They are easily recognizable by disturbing advertising links/banners. Also please ignore resources dealing with jurassic JSF 1.x. They are easily recognizable by using JSP files instead of XHTML files. JSP as view technology was deprecated since JSF 2.0 at 2009 already.
To get started the right way, start at our JSF wiki page and order an authoritative book.