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


what correct way include xhtml page in xhtml page? have been trying different ways, none of them working.

<ui:include>

most basic way <ui:include>. included content must placed inside <ui:composition>.

kickoff example of 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, file in entirety, tags outside <ui:composition> unnecessary ignored 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 opened /page.xhtml. note don't need repeat <html>, <h:head> , <h:body> inside include file otherwise result in invalid html.

you can use dynamic el expression in <ui:include src>. see how ajax-refresh dynamic include content navigation menu? (jsf spa).


<ui:define>/<ui:insert>

a more advanced way of including templating. includes other way round. master template page should use <ui:insert> declare places insert defined template content. template client page using master template page should use <ui:define> define template content inserted.

master template page /web-inf/template.xhtml (as design hint: header, menu , footer can in turn <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 template attribute; here, file in 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 opened /page.xhtml. if there no <ui:define>, default content inside <ui:insert> displayed instead, if any.


<ui:param>

you can pass parameters <ui:include> or <ui:composition template> <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 include/template file, it'll available #{foo}. in case need pass "many" parameters <ui:include>, you'd better consider registering include file tagfile, can use <my:tagname foo="#{bean.foo}">. see when use <ui:include>, tag files, composite components and/or custom components?

you can pass whole beans, methods , parameters via <ui:param>. see jsf 2: how pass action including argument invoked facelets sub view (using ui:include , ui:param)?


design hints

the files aren't supposed publicly accessible entering/guessing url, need placed in /web-inf folder, include file , template file in above example. see which xhtml files need put in /web-inf , not?

there doesn't need markup (html code) outside <ui:composition> , <ui:define>. can put any, ignored facelets. putting markup in there useful web designers. see is there way run jsf page without building whole project?

the html5 doctype recommended doctype these days, "in spite of" it's xhtml file. should see xhtml language allows produce html output using xml based tool. see is possible use jsf+facelets html 4/5? , javaserver faces 2.2 , html5 support, why xhtml still being used.

css/js/image files can included dynamically relocatable/localized/versioned resources. see how reference css / js / image resource in facelets template?

you can put facelets files in reusable jar file. see structure multiple jsf projects shared code.

for real world examples of advanced facelets templating, check src/main/webapp folder of java ee kickoff app source code , omnifaces showcase site source code.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -