jsf 2 - JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output -


i have facelets files below.

 webcontent  |-- index.xhtml  |-- register.xhtml  |-- templates  |    |--userform.xhtml  |    `--banner.xhtml  : 

both pages using templates /templates directory. /index.xhtml opens fine in browser. generated html output. have link in /index.xhtml file /register.xhtml file. however, /register.xhtml not getting parsed , returns plain xhtml / raw xml instead of generated html output. when rightclick page in browser , view page source, still see xhtml source code instead of generated html output. looks template not getting applied.

however, when open /register.xhtml /faces/register.xhtml in browser's address bar, displays correctly. how caused , how can solve it?

there 3 main causes.

  1. facesservlet not invoked.
  2. xml namespace uris missing or wrong.
  3. multiple jsf implemenations have been loaded.

1. make sure url matches facesservlet mapping

the url of link (the url see in browser's address bar) has match <url-pattern> of facesservlet definied in web.xml in order jsf works run. facesservlet 1 responsible parsing xhtml file, collecting submitted form values, performing conversion/validation, updating models, invoking actions , generating html output. if don't invoke facesservlet url, (and see via rightclick, view source in browser) indeed raw xhtml source code.

if <url-pattern> example *.jsf, link should point /register.jsf , not /register.xhtml. if it's example /faces/*, have, link should point /faces/register.xhtml , not /register.xhtml. 1 way avoid confusion change <url-pattern> /faces/* *.xhtml. below ideal mapping:

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

if can't change <url-pattern> *.xhtml reason, prevent endusers directly accessing xhtml source code files url. in case can add <security-constraint> on <url-pattern> of *.xhtml empty <auth-constraint> in web.xml prevents that:

<security-constraint>     <display-name>restrict direct access xhtml files</display-name>     <web-resource-collection>         <web-resource-name>xhtml files</web-resource-name>         <url-pattern>*.xhtml</url-pattern>     </web-resource-collection>     <auth-constraint /> </security-constraint>  

the upcoming jsf 2.3 solve of above automatically registering facesservlet on url pattern of *.xhtml during webapp's startup.

see also:


2. make sure xml namespaces match jsf version

since introduction of jsf 2.2, probable cause xml namespaces don't match jsf version. xmlns.jcp.org below new since jsf 2.2 , not work in older jsf versions. symptoms same if facesservlet not invoked.

<html lang="en"     xmlns="http://www.w3.org/1999/xhtml"     xmlns:f="http://xmlns.jcp.org/jsf/core"     xmlns:h="http://xmlns.jcp.org/jsf/html"     xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> 

if can't upgrade jsf 2.2, need use old java.sun.com xml namespaces instead:

<html lang="en"     xmlns="http://www.w3.org/1999/xhtml"     xmlns:f="http://java.sun.com/jsf/core"     xmlns:h="http://java.sun.com/jsf/html"     xmlns:ui="http://java.sun.com/jsf/facelets"> 

see also:


3. multiple jsf implementations have been loaded

one more probable cause multiple jsf implementations have been loaded webapp, conflicting , corrupting each other. example, when webapp's runtime classpath polluted multiple different versioned jsf libraries, or in specific mojarra 2.x + tomcat 8.x combination, when there's unnecessary configurelistener entry in webapp's web.xml causing loaded twice.

<!-- must remove 1 web.xml! --> <!-- workaround buggy glassfish3 , jetty servers. --> <!-- when leaving in , you're targeting tomcat, you'll run trouble. --> <listener>     <listener-class>com.sun.faces.config.configurelistener</listener-class> </listener> 

when using maven, make absolutely sure declare dependencies right way , understand dependency scopes. importantingly, not bundle dependencies in webapp when provided target server.

see also:


make sure learn jsf right way

jsf has steep learning curve unfamiliar basic http, html , servlets. there lot of low quality resources on internet. please ignore code snippet scraping sites maintained amateurs primary focus on advertisement income instead of on teaching, such roseindia, tutorialspoint, javabeat, etc. recognizable disturbing advertising links/banners. please ignore resources dealing jurassic jsf 1.x. recognizable using jsp files instead of xhtml files. jsp view technology deprecated since jsf 2.0 @ 2009 already.

to started right way, start @ our jsf wiki page , order authoritative book.

see also:


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -