[OmniFaces utilities] The
includeFacelet()
include the Facelet file at the given (relative) path as child of the given UI component parent. This has the same effect as using <ui:include>
. The path is relative to the current view ID and absolute to the web content root.Method:
JSF main page - when the button is clicked, we will include in it some Facelets:
<?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>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Include facelet programmatically"
action="#{faceletsBean.addFaceletAction()}"/>
</h:form>
</h:body>
</html>
JSF pages to
be included. The parent component will be UIViewRoot:
·
fileA.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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<ui:composition>
<h:outputText value="File A|"
style="color: #cc0000;"/>
</ui:composition>
</html>
·
fileB.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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<ui:composition>
<h:outputText value="#{bparam}"
style="color: #00cc00;"/>
</ui:composition>
</html>
·
fileC.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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<ui:composition>
<h:outputText value="#{cparam}"
style="color: #0000cc;"/>
</ui:composition>
</html>
Now, the programmatically
approach is implemented in addFaceletAction() method, as below:
·
using OmniFaces approach (shortcut)
import
org.omnifaces.util.Components;
import
org.omnifaces.util.Faces;
...Components.includeFacelet(Faces.getViewRoot(), "/files/fileA.xhtml");
Faces.getFaceletContext().setAttribute("bparam",
"file B - text as ui:param via string literal...");
Components.includeFacelet(Faces.getViewRoot(),
"/files/fileB.xhtml");
Faces.getFaceletContext().setAttribute("cparam",
cfiletext);
Components.includeFacelet(Faces.getViewRoot(),
"/files/fileC.xhtml");
·
using classical approach
FacesContext
context = FacesContext.getCurrentInstance();
FaceletContext
faceletContext = (FaceletContext)
context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
faceletContext.includeFacelet(context.getViewRoot(),
"/files/fileA.xhtml");
faceletContext.setAttribute("bparam",
"file B - text as ui:param via string literal...");
faceletContext.includeFacelet(context.getViewRoot(),
"/files/fileB.xhtml");
faceletContext.setAttribute("cparam",
cfiletext);
faceletContext.includeFacelet(context.getViewRoot(),
"/files/fileC.xhtml");
In both
cases the output will be like in figure below (left side - before inclusion,
right side - after inclusion):
P.S. You may
be also interested in "Dynamically load content from WEB-INF in PrimeFaces center layout unit using Facelets and AJAX".
Niciun comentariu :
Trimiteți un comentariu