Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

JSF Question


Hi all,

I want to create a page like Master Page in JSF application. I have header, footer, sidemenu, rightmenu, and main page in between.

Even if the content of main page is too large, the footer and other things should look perfect, without overriding eachother.

Any help regarding this will be appreciated.

Thanks in advance,
JSF GEEKS

Your rating: None Average: 5 (3 votes)

You could use tiles with JSF

You could use tiles with JSF to fulfill your requirement. here is a outline of what could be done.

1. Add tiles descriptor in your webapp : To do this ,in your web.xml specify the context init param javax.faces.application.CONFIG_FILES. Set the param value to the path of the web application configuration files.
Here is an example:

<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml, /WEB-INF/tiles-defs.xml</param-value>
</context-param>

2. Also you need to add Tiles framework to your webapp by specifying the Tiles Servlet in the web.xml. Here is an example

<servlet>
<servlet-name>Tiles Servlet</servlet-name>
<servlet-class>
org.apache.struts.tiles.TilesServlet
</servlet-class>
<init-param>
<param-name>definitions-config</param-name>
<param-value>/WEB-INF/tiles-defs.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

3. Copy the struts-tiles.tld to the WEB-INF directory. The namespace for the tags defined in this tld is is http://jakarta.apache.org/struts/tags-tiles

4. Now create your JSPs
4.1 body.jsp : this is the jsp which will contain the body
4.2 header.jsp : this shows the header
4.3 footer.jsp : the footer you want to show
4.4 tiles.jsp : this is the wrapper over body.jsp. you need to hit this page in app. example :

<%@ taglib uri="http://java.sun.com/jsf/core"  prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html"  prefix="h" %>
   <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<html>
   <f:view>
      <head>      
      </head>
      <body>
         <f:subview id="body">
            <h:form>
               <tiles:insert definition="layout" flush="false">
                  <tiles:put name="table" value="body.jsp"/>
              </tiles:insert>
            </h:form>
         </f:subview>
      </body>
   </f:view>
</html>

4.5 layout.jsp : to layout your elements...
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<tiles:importAttribute scope="request"/>
<h:panelGrid>
<f:subview id="header">
  <tiles:insert attribute="header" flush="false"/>
</f:subview>

<f:subview id="table">
  <tiles:insert attribute="table" flush="false"/>
</f:subview>

<f:subview id="footer">
  <tiles:insert attribute="footer" flush="false"/>
</f:subview>
</h:panelGrid>

5. Next create tiles-defs.xml and put it in WEB-INF
Example tiles-defs.xml is :

<tiles-definitions>
<definition name="layout" path="/layout.jsp">
<put name="header" value="/header.jsp"/>
<put name="footer" value="/footer.jsp"/>
</definition>
</tiles-definitions>

6. you will need the following jars .
For Tiles/JSTL : standard.jar, jstl.jar,
Others : jsf-impl.jar, jsf-api.jar, struts.jar,
commons-beanutils.jar,
commons-digester.jar,
commons-collections.jar,
commons-logging.jar

For more information http://www.jsftutorials.net/tiles/jsf-tiles.html

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.