60 likes | 84 Views
IS-907 Java EE. Templating. Templates. When an application contains several pages with similar structure, duplication of code can be avoided through the use of templates The template defines the structure of the page with named parts The template may specify default content for each part
E N D
IS-907 Java EE Templating
Templates • When an application contains several pages with similar structure, duplication of code can be avoided through the use of templates • The template defines the structure of the page with named parts • The template may specify default content for each part • Facelets which use the template must specify the named parts • Default content is used for unspecified parts Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon
Defining templates • The template is an ordinary facelet, except ui:insert tags are used to specify replacable parts: <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:outputStylesheet library="css" name="is907.css" /> </h:head> <h:body> <div id="heading" class="heading"> <ui:insert name="heading”/> </div> ... Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon
Defining default content • The ui:include tag is used to include default content from other files: <div id="sidebar" class="menu"> <ui:insert name="sidebar"> <ui:include src="/views/common/sidebar.xhtml" /> </ui:insert> </div> Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon
Using templates <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"> <h:head> <title>IGNORED</title> </h:head> <h:body> <ui:composition template="/views/templates/layout.xhtml"> <ui:define name="title">IS-907 Netshop - Price List</ui:define> <ui:define name="content"> <h1>Price List</h1> ... </ui:define> </ui:composition> Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon
Using templates • Facelets refer to templates with a ui:composition tag • and specify the parts with ui:define tags • Only the contents of the ui:composition tag are used • the rest of the file is ignored • the structure of the page is taken from the template • Included files (e.g. default parts) are treated the same way: • Only the contents of the ui:composition tag are used Even Åby Larsen (even.larsen@uia.no) IS-102 Introduksjon