You are on page 1of 3

How to create master page in Java/JSP ?

Posted by Vicky Thakor 16 July 2011 Hello Java developer you face many problems regarding the developing site and you have to maintain design for all the page as a same. ASP .Net supports master page but what about JSP. I'll show you how you can create master page for your site. The API that helps to create the master page is "SiteMesh" developed by Open Symphony. You want to check out the site click here . We'll discuss sitemesh 2 series. Yet not tested sitemesh 3. Download JSTL Sitemesh is a java web application development framework developed by Open Symphony. Some time website uses the same menu and other content to all pages. Example like Menubar in head of the page it'll be used on every page ti navigate the site. Template(Master Page) comes here to apply certain design to every page of domain. How it works?

Steps to create the master page. Step 1: Create blank JSP page. Include tag library of sitemesh at head of the page. <%@taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> Include tag library of JSTL at head of the page. <%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@taglib uri="http://java.sun.com/jstl/fmt" prefix="fn" %> Step 2: To use the title of the html page that will be displayed using sitemesh. - Title of the page Sample code.

<%@taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title><decorator:title/></title> </head> <body> <decorator:body/> </body> </html> Step 3: Configure the sitemesh to pass request of the page through the template. Create decorators.xml file in WEB-INF folder. <?xml version="1.0" encoding="UTF-8"?> <decorators defaultdir="/template"> <decorator name="main" page="template1.jsp"> <pattern>/Folder_name/*</pattern> </decorator> </decorators> defaultdir Path of template page page Template that will be applied to pages - The URL pattern (Directory) on which the template will be applied if we use /* so all request pages pass through the template and itll be applied to all page. Step 4: Configure the web.xml file Append the below code in web.xml file. <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> - Template applied to all page extension. - Template applied to only on html page.

You might also like