0

I'm quite new to using JSF and I'm not sure if that's the right way to go, but in Rails you usually have a main application file into which the current page is loaded. That way I don't have to worry about copy-pasting the menu, etc. every time.

How can I achieve that with JSF 2? Can I navigate to the same main page every time and tell it to load a current content? Or do I tell the current page that I navigate to to load the "main frame around the content"?

Thanks!

Pete
  • 10,720
  • 25
  • 94
  • 139

2 Answers2

3

Yes of course, JSF 2.0 has page-templating feature. You define a template that defines a generic layout to all the view pages.

Facelets tags to create basic page:

  • ui:insert – defines content that is going to replace by the file that load the template;
  • ui:define – defines content that is inserted into tag ui:insert;
  • ui:include – includes content from another page;
  • ui:composition – the specified template is loaded, if used with template attribute, and the children of this tag defines the template layout. In other case, it’s a group of elements, that can be inserted somewhere.

For example:

<ui:composition
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:ui="http://java.sun.com/jsf/facelets"
    template="/templates/myLayout.xhtml">

   <ui:define name="menu">
      <ui:include src="/mypath/menu.xhtml"/>
   </ui:define>

   <ui:define name="content"> 
     <ui:include src="/mypath/content.xhtml"/>           
   </ui:define>

</ui:composition>

or

<ui:insert name="content">
   <ui:include src="/mypath/mycontent.xhtml"/>
</ui:insert>
kapandron
  • 3,546
  • 2
  • 25
  • 38
1

JSF doesn't support what you want to archive. Instead, it support the views and basic layout(template). What you need it this:

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
template="path/to/template.xhtml>

<your custom content here/>
<ui:composition/>
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103