Extract XML namespace declarations into separate tagfile/taglib - jsf

I would like to seperate my XML namespace declarations at the top of the page into a seperate (tag)file in which I can include in any Facelets page I wish.
Is this possible? Or do I need to copypaste all XML namespaces in each Facelets file?
Here's a theoretical example, xmlns.xhtml:
<something
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:c="http://java.sun.com/jsp/jstl/core"
...
/>
And the template file which could use it: mypage.xhtml:
<f:view xmlns="xmlns.xhtml">
<h:head>
...
</h:head>
<h:body>
...
</h:body>
</f:view>

Nope, not possible. They must be present during XML compile time already.
Your best bet is creating or configuring XHTML template(s) in your IDE so that you can choose from them in New File without the need to copypaste/retype the boilerplate.
Here are some screens from Eclipse + JBoss Tools:

Related

ui:include includes wrong file in JSF 2.2

Accorrding to the documentation of ui:include tag
Use this tag—which is very similar to JSP's jsp:include—to encapsulate
and reuse content among multiple XHTML pages. There are three things
this tag can include: plain XHTML, and XHTML pages that have either a
composition tag or a component tag.
You supply a filename, through ui:include's src attribute for JSF to
include. That filename is relative to the XHTML file that was rendered
as a result of the last request. So, for example, if JSF loaded the
view login.xhtml, and that file included pageDecorations/header.xhtml,
and pageDecorations/header.xhtml included companyLogo.xhtml, then
companyLogo.xhtml will not be found if it's in the pageDecorations
directory, because companyLogo.xhtml has to be in the same directory
as login.xhtml.
I created a simple test:
webapp/login.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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<ui:include src="pageDecorations/header.xhtml" />
</h:body>
</html>
webapp/pageDecorations/header.xhtml
<ui:include
src="logo.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
/>
webapp/pageDecorations/logo.xhtml
<h:outputText
value="Logo in /pageDecorations"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
/>
webapp/logo.xhtml
<h:outputText value="Logo in /"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
/>
When I ran this test (loaded login.xhtml page) using JSF 2.2 on WildFly 10.1 , I got Logo in /pageDecorations, while according to the documentation it should be: Logo in /
What is wrong ? Is there a bug in the documentation or Mojarra implementation ? Or my understanding is wrong ?
First, you are inside webapp/ and executing webapp/login.xhtml
inside that you get src="pageDecorations/header.xhtml" and for exectuing that you are in pageDecorations and from header.xhtml you are tring to find src="logo.xhtml" which will you get in the same directory (pageDecorations) so it will print
"Logo in /pageDecorations" .
Document looks wrong in this case.

JSF: How to insert something into a custom facelets tag?

I have created a custom facelets tag file, but I'm struggeling to insert something into it. This is the tag:
<ui:composition 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">
<h1>TestTag</h1>
<ui:insert name="foo"/>
</ui:composition>
This is how I expect to use it:
<ds:testtag>
<ui:define name="foo">
<h2>TestInsert</h2>
</ui:define>
</ds:testtag>
Of course, I have created a taglib file and registered it in web.xml. The ds: namespace is also declared in the file where I want to use the tag.
On the rendered result I can see the TestTag caption from the tag itself, but not the inserted TestInsert.
The answer to this question How to create a custom Facelets tag? as well as a comment here How to create a composite component for a datatable column? suggests that it is possible to insert something into a tag; unfortunately, I couldn't find a working example. What am I missing?
Try this (not tested):
<ui:composition 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">
<h1>TestTag</h1>
<ui:insert />
</ui:composition>
And use component like this:
<ds:testtag>
<h2>TestInsert</h2>
</ds:testtag>
You need insert into component a tag:
<ui:composition 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">
<h1>TestTag</h1>
<ui:insertChildren/>
</ui:composition>
And when use it like this:
<ds:testtag>
<h2>TestInsert</h2>
</ds:testtag>
Also you may use
<composite:insertChildren/>
Don't forget for right imports
xmlns:composite="http://java.sun.com/jsf/composite"
or
xmlns:ui="http://java.sun.com/jsf/composite"

jsf - facelets - pack library jar with *.xhtml-s and beans only

I could read the tutorial which describes how to pack facelets to jar to reuse tags as library...
The thing is the instruction recommends using both JSP and JSF in manner as (see code snippet);
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" version="2.0">
<ui:composition>
...
</ui:composition>
</jsp:root>
...and using both .taglib.xml + .tld conf files... :(
So I don't get it may ui:composition in *.xhtml be started as usual by (see code snippet) ...
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" version="2.0">
...
</ui:composition>
...not to deal with jsp at all? And if, yes, how this kind of library can be added to dynamic web project in eclipse for example?
So my question concerning the tutorial is...
A) is there a way to avoid using *.tld and use have *.taglib.xml only for the lib if the tag library will contain xhtml files only?
B) is there a way to have xhtml-s in non META-INF sub-folder but in some random one?
C) How to automate this kind of library creation process? I mean should I create a project in eclipse (?project category) to have correct folders structure by default to make possible re-compile it and test it and only after that pack it as a jsf library?
I googled a lot but couldn't find solutions variations so please share your experience;
Thanks

Implementing JSF Composite Component in Maven project

It seems people are able to implement composite components in SWF 2.3.1, but I cannot find a clear reference for how this is done. I have followed the basic structure for a JSF composite component, but my SWF application does not seem to recognize the taglib namespace.
There is a toolkit/IDE warning, but more importantly there is a runtime warning seen in the browser, JSF is displaying the following warning on the screen to the user:
Warning: This page calls for XML namespace http://java.sun.com/jsf/composite/myjsf declared with prefix mj but no taglibrary exists for that namespace.
Component definition:
src/main/resources/myjsf/testComponent.xhtml :
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:attribute name="attr" />
</composite:interface>
<composite:implementation>
#{cc.attrs.attr});
</composite:implementation>
</html>
Referenced in a given xhtml:
<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"
xmlns:mj="http://java.sun.com/jsf/composite/myjsf">
<!-- snip -->
<mj:testComponent attr="x" />
</ui:composition>
The folder src/main/resources/myjsf/ isn't right place for your composite component. Composite component in maven projects should be in src/main/webapp/resources/ and in your case it should be src/main/webapp/resources/myjsf/testComponent.xhtml.
As you are using Maven you should know that webapp folder is folder whose content will be deployed in root folder of your application, and thus it is somehow analogue as WebContent folder in standard dynamic web project in Eclipse.

How to include another XHTML in XHTML using JSF 2.0 Facelets?

What is the most correct way to include another XHTML page in an XHTML page? I have been trying different ways, none of them are working.
<ui:include>
Most basic way is <ui:include>. The included content must be placed inside <ui:composition>.
Kickoff example of the master page /page.xhtml:
<!DOCTYPE html>
<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">
<h:head>
<title>Include demo</title>
</h:head>
<h:body>
<h1>Master page</h1>
<p>Master page blah blah lorem ipsum</p>
<ui:include src="/WEB-INF/include.xhtml" />
</h:body>
</html>
The include page /WEB-INF/include.xhtml (yes, this is the file in its entirety, any tags outside <ui:composition> are unnecessary as they are ignored by Facelets anyway):
<ui:composition
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">
<h2>Include page</h2>
<p>Include page blah blah lorem ipsum</p>
</ui:composition>
This needs to be opened by /page.xhtml. Do note that you don't need to repeat <html>, <h:head> and <h:body> inside the include file as that would otherwise result in invalid HTML.
You can use a dynamic EL expression in <ui:include src>. See also How to ajax-refresh dynamic include content by navigation menu? (JSF SPA).
<ui:define>/<ui:insert>
A more advanced way of including is templating. This includes basically the other way round. The master template page should use <ui:insert> to declare places to insert defined template content. The template client page which is using the master template page should use <ui:define> to define the template content which is to be inserted.
Master template page /WEB-INF/template.xhtml (as a design hint: the header, menu and footer can in turn even be <ui:include> files):
<!DOCTYPE html>
<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">
<h:head>
<title><ui:insert name="title">Default title</ui:insert></title>
</h:head>
<h:body>
<div id="header">Header</div>
<div id="menu">Menu</div>
<div id="content"><ui:insert name="content">Default content</ui:insert></div>
<div id="footer">Footer</div>
</h:body>
</html>
Template client page /page.xhtml (note the template attribute; also here, this is the file in its entirety):
<ui:composition template="/WEB-INF/template.xhtml"
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">
<ui:define name="title">
New page title here
</ui:define>
<ui:define name="content">
<h1>New content here</h1>
<p>Blah blah</p>
</ui:define>
</ui:composition>
This needs to be opened by /page.xhtml. If there is no <ui:define>, then the default content inside <ui:insert> will be displayed instead, if any.
<ui:param>
You can pass parameters to <ui:include> or <ui:composition template> by <ui:param>.
<ui:include ...>
<ui:param name="foo" value="#{bean.foo}" />
</ui:include>
<ui:composition template="...">
<ui:param name="foo" value="#{bean.foo}" />
...
</ui:composition >
Inside the include/template file, it'll be available as #{foo}. In case you need to pass "many" parameters to <ui:include>, then you'd better consider registering the include file as a tagfile, so that you can ultimately use it like so <my:tagname foo="#{bean.foo}">. See also When to use <ui:include>, tag files, composite components and/or custom components?
You can even pass whole beans, methods and parameters via <ui:param>. See also JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)?
Design hints
The files which aren't supposed to be publicly accessible by just entering/guessing its URL, need to be placed in /WEB-INF folder, like as the include file and the template file in above example. See also Which XHTML files do I need to put in /WEB-INF and which not?
There doesn't need to be any markup (HTML code) outside <ui:composition> and <ui:define>. You can put any, but they will be ignored by Facelets. Putting markup in there is only useful for web designers. See also Is there a way to run a JSF page without building the whole project?
The HTML5 doctype is the recommended doctype these days, "in spite of" that it's a XHTML file. You should see XHTML as a language which allows you to produce HTML output using a XML based tool. See also Is it possible to use JSF+Facelets with HTML 4/5? and JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used.
CSS/JS/image files can be included as dynamically relocatable/localized/versioned resources. See also How to reference CSS / JS / image resource in Facelets template?
You can put Facelets files in a reusable JAR file. See also Structure for multiple JSF projects with shared code.
For real world examples of advanced Facelets templating, check the src/main/webapp folder of Java EE Kickoff App source code and OmniFaces showcase site source code.
Included page:
<!-- opening and closing tags of included page -->
<ui:composition ...>
</ui:composition>
Including page:
<!--the inclusion line in the including page with the content-->
<ui:include src="yourFile.xhtml"/>
You start your included xhtml file with ui:composition as shown above.
You include that file with ui:include in the including xhtml file as also shown above.

Resources