How to make facelets conform to XHTML 1.0 Transitional? - jsf

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.

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.

JSF Input Text Placeholder

I am using JSF 2.2 and I am trying to put a placeholder on some input text.
<h:inputText value="#{loginBean.studentId}" id="username" a:placeHolder="Username"/>
I have placed the following in the head of my xhtml document and added the
pass through name space:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"htth://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"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
However, I receive the following warning and the placeholder does not render on my page:
Can't find facelet tag library for uri http://xmlns.jcp.org/jsf/passthrough
Thanks for any help and advise.

How to use composite component

I'm create mycomponent.xhtml in the root of Web Pages directory. In mycomponent.xhtml i describe some composite component
<?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:composite="http://java.sun.com/jsf/composite">
<composite:interface/>
<composite:implementation>
...
</composite:implementation>
</html>
Now i'm creating facelet index.html in the root of Web Pages directory. But i cannot use mycomponent
<?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:mycomp="http://java.sun.com/jsf/composite">
<mycomp:mycomponent/> <!--Error, the component library composite does not contain mycomponent-->
</html>
The composite component page (mycomponemt.xhtml) that you placed at the root must be located at /resources/mycomp folder: see composite components documentation for details.
You can find a kickoff example in Oracle's Java EE tutorial. Another one can be found in our composite component tag wiki.

Unknown jsf tag in xhtml page in jsf application

This is my first application with eclipse and jsf and i having some problems.
Here is my project structure:
eclipse project structure
I have downloaded and used JSF 2.1 (Mojarra 2.1.6-FCS) jar file and there is only a single jar file in that and i have used that jar file as a user library.
Now the problem i am facing is that when i use h:head in my index.xhtml file it shows that h:head is an unkown tag.Of course i am using anugular brackets. besides h:head , i am not able to type that in here.
here is my index.xhtml file:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</h:head>
<body>
</body>
</html>
What is wrong here? IS there only one jar file file to be included?
also there are some white packages marks in the jar file i included...here they are:
while package in jar
what does it mean?
The problem is that you haven't defined the h prefix anywhere in your file. If you follow a JSF 2 tutorial, you will note this in the <html> tag definition (this one is taken from StackOverflow JSF wiki):
<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">
Where
h for JSF HTML tags like <h:head>, <h:body>, <h:inputText>, etc.
f is the prefix for JSF core like <f:view>, <f:selectItems>, <f:ajax>, etc.

HTML doctype declaration in 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

Resources