| Sign In/My Account | View Cart |
Advanced SiteMesh
Pages: 1, 2, 3
PageDecoratorMapperA page can override which decorator should be used by specifying the decorator name through the META attribute.
In order to use this mapper, you have to change sitemesh.xml:
<mapper
class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
</mapper>
PageDecoratorMapper takes a list of parameters. In our example,
we are instructing it that the value of decorator's META property should be used
as the decorator name. So if I want to use a decorator named test
for the page, all I have to do is add the following line in the header:
<META name="decorator" content="test">
PageDecoratorMapper provides a static approach for a page to
choose which decorator should be used. A page can decide which decorator to use
at runtime by using ParameterDecoratorMapper.
ParameterDecoratorMapperTo use a ParameterDecoratorMapper, add an entry for it in
sitemesh.xml:
<mapper
class= "com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper">
<param name="decorator.parameter" value="decorator" />
<param name="parameter.name" value="confirm" />
<param name="parameter.value" value="true" />
</mapper>
This takes three parameters:
decorator.parameter
The name of the request parameter used for specifying the name of the decorator.
parameter.name
The name of the request parameter used for confirming the decorator parameter.
parameter.value
The value of the request parameter used for confirming the decorator parameter.
If you want help.jsp to use the test decorator, then you can
invoke it like this:
help.jsp?decorator=test&confirm=true
SiteMesh provides a few more mappers that are very helpful. Some of these are:
FrameSetDecoratorMapper
Should be used when page is a frame.
CookieDecoratorMapper
Lets you use a cookie value for specifying the name of the decorator to be used.
RobotDecoratorMapper
Will use the specified decorator when the requester is identified as a robot. You may want to add a keywords header
if the page is accessed by a robot, and a decorator is a good way to do that.
SiteMesh does not restrict you to use JSP while creating decorators. You are free to use either Velocity or Freemarker. Velocity and Freemarker are templating languages that can be used for creating web pages. These languages are much easier to use than normal JSP, but are not as programmable as JSP.
SiteMesh adds supports for these two templating languages through two servlets, which are part of SiteMesh.jar. We have to declare these two servlets in web.xml, as follows:
<servlet>
<servlet-name>sitemesh-velocity</servlet-name>
<servlet-class>
com.opensymphony.module.sitemesh.velocity.VelocityDecoratorServlet
</servlet-class>
</servlet>
<!--Declare servlet for handling freemarker requests -->
<servlet>
<servlet-name>sitemesh-freemarker</servlet-name>
<servlet-class>
com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet
</servlet-class>
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<init-param>
<param-name>default_encoding</param-name>
<param-value>ISO-8859-1</param-value>
</init-param>
</servlet>
<!-- Velocity servlet should serve all requests with .vm extension-->
<servlet-mapping>
<servlet-name>sitemesh-velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
<!-- FreeMarker servlet should serve all requests with .dec extension-->
<servlet-mapping>
<servlet-name>sitemesh-freemarker</servlet-name>
<url-pattern>*.dec</url-pattern>
</servlet-mapping>
We also have to copy freemarker.jar,
velocity-dep.jar, and velocity-tools-view.jar into the
lib folder. These .jars are shipped with the SiteMesh distribution. We
will change our first sample for using Velocity and Freemarker decorators
instead of JSP. In our first sample we have two decorators: headerfooter and
sidemenu. We will create headerfooter.dec like this:
<html>
<head>
<title>My Site - $Advanced SiteMesh</title>
${head}
</head>
<body>
<table border="1">
<tr>
<td>SiteMesh Corporation</td>
</tr>
<tr>
<td>${body}</td>
</tr>
<tr>
<td>SiteMesh copyright</td>
</tr>
</table>
</body>
</html>
In this page, we are using Freemarker templates for header, footer, and title
instead of custom tags, but the page layout is the same. When the container
receives a request for a page with a .dec extension, it will pass it to
FreemarkerDecoratorServlet, which invokes
FreemarkerDecorator for generating the final HTML page. We are
using the $Advanced SiteMesh template for accessing the title of the web page
generated by the application, ${head} for accessing the head, and
${body} for accessing the body. Freemarker provides more such
templates. You can find out more about it in the Resources
section below.
We also need to create sidemenu.vm in the decorators folder. This is the Velocity decoration file.
<html>
<head>
<title>My Site - $title</title>
$head
</head>
<body>
<table border="1">
<tr>
<td> SiteMesh Header </td>
</tr>
<tr>
<td> Sidemenu </td>
<td> $body </td>
</tr>
<tr>
<td> SiteMesh Footer </td>
</tr>
</table>
</body>
</html>
For a Velocity decorator, we need to replace decorator:title
with a $title template. Similarly, we will use head
and body Velocity templates instead of custom tags.
SiteMesh is a very flexible and easy-to-use decoration framework based on filters. But it has a couple of problems. First, filters were introduced in the Servlet 2.3 specification, so older application servers may not support them. Please check that your application is going to be deployed on a container that supports filters.
Also, a filter gets control only if a user requests that page. If you
call home.jsp through a browser, it will be decorated, but if one of your
servlets gives control to home.jsp through either
RequestDispatcher.include() or forward(), it won't get
decorated. But don't worry--in the Servlet 2.4 specification and up, you can just
configure filters to be invoked in forwarding and include cases, as well as for
regular requests.
Sunil Patil has worked on J2EE technologies for more than five years. His areas of interest include object relational mapping tools, UI frameworks, and portals.
Return to ONJava.com.
Showing messages 1 through 2 of 2.
I am trying to user sitemesh with JSF, I am facing a typical problem with sitemesh and JSF,
Can somebody please tell me where am I going wrong
my Decorator.jsp is as follow
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page"%>
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<page:applyDecorator page="/jsp/decorator/header.jsp" name="panel" title="menu"/>
</td>
</tr>
<tr>
<td>
<decorator:body/>
</td>
</tr>
</table>
and header.jsp is as follow
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:view>
<table>
<tr>
<td>
<h:outputText id="userName" value="Test Value"/>
</td>
</tr>
</table>
</f:view>
my faces servlet mapping and sitemesh mappings are as follow (in web.xml)
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
If put f:view tag in header.jsp I get the following error
com.opensymphony.module.sitemesh.taglib.page.ApplyDecoratorTag.doEndTag(ApplyDecoratorTag.java:271)
instead if I remove all JSF tags like <f:view> and <h:outputText> then it works fine
I tried to apply the decorator using ýpage=faces/jsp/decorator/header.jspý in decorator.jsp, since faces servlet mapping uses faces/* (in web.xml)
any configuration problem or something else ??
One more thing is
Instead of <page:applyDecorator> if I use <%@ include file="jsp/decorator/header.jspý " %>
It is throwing faces context exception
Please help me
PS: please refer http://forums.java.sun.com/thread.jsp?thread=526351&forum=427&message=2807796
for the similar posting.
Thanks and Regards,
Gururaj Kulkarni
IonIdea Enterprise Solutions Pvt. Ltd.
#38-40, Export Promotion Industrial Park,
Whitefield, Bangalore - 560066.