HTML doctype declaration in JSF - jsf

I have a very strange problem in JSF page that I cannot solve. I have this html doctype declaration into the xhtml page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<ui:insert name="header">
<ui:include src="header.xhtml"/>
</ui:insert>
</h:head>
<h:body>
When I run the JSF page and I open Furebug to the page code I get this:
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body>
</html>
It should be like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<body>
</html>
I don't know why the html doctype is skipped? Can you help me to fix this?
P.S I use JSF navigation to navigate to the new page:
<h:commandButton id="newdatacenter" styleClass="lbimage" value="New Datacenter" action="#{DatacentersController.navigateToNewDatacenter()}">
// <f:ajax render="#form"></f:ajax>
</h:commandButton>
// Navigate to New Datacenter page
public int navigateToNewDatacenter(){
return 11432;
}
<navigation-rule>
<description>Navigation rule to New Datacenter page</description>
<from-view-id>/DatacentersList.xhtml</from-view-id>
<navigation-case>
<from-action>#{DatacentersController.navigateToNewDatacenter()}</from-action>
<from-outcome>11432</from-outcome>
<to-view-id>/NewDatacenter.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
I don't know why when the new JSF page is opened the HTML doctype declaration is not included.

Recent Mojarra versions have a bug which causes the doctype to be removed in the rendered HTML. This seems to occur if included files define a doctype on their own. If you use an affected version (<2.1.14), you might want to check
Doctype not rendered in the generated HTML if doctype was defined in an included file

Related

How to use JSF templates stored in a shared jar?

I am trying to store a set of JSF templates and other resources in a .jar file, which several webapps will draw from. I found this SO answer, which seems to provide instructions for the very thing I'm trying to do. However, while I can load images from the shared jar, I cannot get JSF templates to work. I get a file not found error.
I'm using Open Liberty 18.0.0.4, and the jar is stored in the ${server.config.dir}/lib/global directory, which I understand makes the contents available to all webapps.
Here is the output of jar tf of the shared jar.
META-INF/MANIFEST.MF
META-INF/resources/common/images/ufo.png
META-INF/resources/common/jsf/template.xhtml
This is the template.xhtml from the shared jar.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Title from template</title>
</h:head>
<h:body>
This is content from the template<br/>
</h:body>
</html>
Turning to my webapp, here is the directory layout.
/WEB-INF
/WEB-INF/classes
/WEB-INF/web.xml
/META-INF
/imageTest.xhtml
/localTemplate.xhtml
/localJSFTest.xhtml
/sharedJarJSFTest.xhtml
The imageTest.xhtml simply displays the ufo.png from the shared jar, and this facelet works without a problem.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Test Image</title>
</h:head>
<h:body>
I can see this image.<br/>
<h:graphicImage library="common" name="images/ufo.png" alt="ufo"/>
</h:body>
</html>
The localJSFTest.xhtml uses the localTemplate.xhtml, which is part of the webapp. This also works.
localJSFText.html
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:body>
<ui:composition template="localTemplate.xhtml" />
</h:body>
</html>
localTemplate.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Title from local template</title>
</h:head>
<h:body>
This is content from the LOCAL template<br/>
</h:body>
</html>
The problem comes when sharedJarJSFText.xhtml tries to use template.xhtml (posted above) from the shared jar.
sharedJarJSFText.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:body>
<ui:composition template="/common/jsf/template.xhtml" />
</h:body>
</html>
I get the following error when I try to access sharedJarJSFText.xhtml in the browser.
/common/jsf/template.xhtml Not Found in ExternalContext as a Resource
viewId=/sharedJarJSFTest.xhtml
location=/home/jmac/Programming/NetBeansProjects/TestJSFWebApp/src/main/webapp/sharedJarJSFTest.xhtml
phaseId=RENDER_RESPONSE(6)
Caused by:
java.io.FileNotFoundException - /common/jsf/template.xhtml Not Found in ExternalContext as a Resource
at org.apache.myfaces.view.facelets.impl.DefaultFaceletFactory.resolveURL(DefaultFaceletFactory.java:300)
I think I'm following the instructions in the above linked SO answer, so I'm not sure why this isn't working.
In another SO answer, user BalusC explains that since the Servlet 3.0 spec writing a custom ResourceResolver is unnecessary. This version of Open Liberty uses the Servlet 4.0 spec.
If helpful, here is the feature list from the server.xml for this Open Liberty server.
<featureManager>
<feature>jsp-2.3</feature>
<feature>jsf-2.3</feature>
</featureManager>
Thanks in advance for any suggestions.

Component library HTML basic does not contain datatable

I am creating a datatable using JSF pages but when I am trying to create one I am getting an error Component library HTML basic does not contain datatable, I have no idea why it doesn't contain this feature. I couldn't find anything to help this error through other sources.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " "http: //www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<ui:composition template="/Template.xhtml" >
<ui:define name="content">
<h:panelGrid>
<h:datatable value ="#{//somecode}">
</h:panelGrid>
</ui:define>
</ui:composition>
</h:body>
</html>
I was using datatable instead of the component dataTable

ui:insert is not rendering in the main template

After a year gap i have again started working in JSF and facing problem in the facelets usage Below is the maintemplate.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title><ui:insert name="title" /></title>
</h:head>
<ui:insert name="header" />
<body>
content
</body>
</html>
Below is the ui:composition of the other xhtml file
enter code here
<?xml version="1.0" encoding="UTF-8" ?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="/index.xhtml">
<ui:define name="title"><h:outputText value="Please Sign In" /></ui:define>
<ui:define name="header"><h:outputText value="Please Sign In" /></ui:define>
</ui:composition>
the same content works with the ui:include but doesn't work with the ui:defin and ui:insert combination
I was using the tags in the wrong way need to include include tag inside the insert to get the correct template functionality

How to make facelets conform to XHTML 1.0 Transitional?

Why can't facelets XHTML files be valid XHTML 1.0 Transitional files?
If I submit a facelet file in an xml validator (e.g. w3c validator) it shows an error on the first tag defined in one of the taglibs.
Example 1:
If I submit the following file to the validator, it shows no error, validation goes fine because no taglib tags appear in the document (one jsf taglib is defined though).
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
<head>
<title></title>
</head>
<body>
</body>
</html>
Example 2:
Now I just inserted the h:head and h:body tags from the xmlns:h namespace but this causes errors in the validation.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title></title>
</h:head>
<h:body>
</h:body>
</html>
Certainly I'm missing something fundamental here, but I still can't figure out what.
This is my first question here at S.O. so please point out any errors, thank you!
If you check the Lifecycle of a Facelets Application you can see that your view is rendered to the client. This will transform tags like <h:head> replacing them with their xhtml equivalent e.g. <head>. If you want templates that are XHTML valid you may want to try with the jsfc attribute but it has its drawbacks.

Calling another page in JSF

I have a simple JSF application with 2 .xhtml files. When I run the application, the 1st page displayed is welcome.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Final Project</title>
</h:head>
<h:body bgcolor="white">
<div align="center" style="border:5px outset blue;">Welcome to the Product Inventory Application</div>
<br></br>
<br></br>
<h:commandButton value="View All Products" action="allProducts"/>
</h:body>
It displays fine, but when I press the View All Products button, I expect it to display the allProducts.xhtml facelet. But when I click the button, nothing happens at all, no exception or anything. The allProducts.xhtml page is just:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>All Products</title>
</h:head>
<h:body bgcolor="white">
<h3>Test</h3>
</h:body>
</html>
The problem is that an UICommand (<h:commandButton>, <h:commandLink> and similars) must be inside a form i.e. <h:form>. Change your welcome.xhtml page to:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Final Project</title>
</h:head>
<h:body bgcolor="white">
<div align="center" style="border:5px outset blue;">Welcome to the Product Inventory Application</div>
<br></br>
<br></br>
<h:form>
<h:commandButton value="View All Products" action="allProducts"/>
</h:form>
</h:body>
More info:
h:commandLink / h:commandButton is not being invoked, reason 1. IMO, I suggest you to mark this question as a favorite if you're going to work a lot with JSF :).
I don't know the precise detail by heart, but here is the general idea. The action attribute of the button on the welcome page is referring to an 'allProducts' method of the backing bean. That method would have to return the string 'allProducts.xhtml' in order to have JSF present the products page.
So you have to introduce a backing bean for the welcome page and endow that class with a method 'allProducts'.

Resources