SiteMesh - Freemarker Decorators

本文介绍如何在SiteMesh中使用Freemarker模板作为装饰器。通过具体配置步骤及示例代码,展示了如何安装Freemarker并将其与SiteMesh整合,实现网页布局的统一管理和重用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SiteMesh - Freemarker Decorators

As of SiteMesh 2.0.2 Freemarker (.ftl) decorators are supported.

Here is an example of how such a decorator might look like:

<#include "/includes/decorators/header.dec">
    <h2>${title}</h2>
    ${head}
    <img src="${base}/images/logo.gif" border="0">
    <td valign="top" class="body">
        <div class="header">
            <span class="pagetitle">${title}</span>
        </div>
        ${body}
    </td>
<#include "/includes/decorators/footer.dec">

Installation

  • Download Freemarker 2.3 (recommended) and copy it into [web-app]/WEB-INF/lib. The SiteMesh distribution comes with freemarker.jar v2.3rc3

     

  • Add the following to [web-app]/WEB-INF/web.xml within the <web-app> tag:
<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>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>sitemesh-freemarker</servlet-name>
    <url-pattern>*.dec</url-pattern>
</servlet-mapping>
  • Modify decorators.xml to reference a .dec file.
<#include "/includes/decorators/header.dec">
    <h2>${title}</h2>
    ${head}
    <img src="${base}/images/logo.gif" border="0">
    <td valign="top" class="body">
        <div class="header">
            <span class="pagetitle">${title}</span>
        </div>
        ${body}
    </td>
<#include "/includes/decorators/footer.dec">

Context

FreemarkerDecoratorServlet puts some things into the context object that you should be aware of:

Basic context attributes

 

  • It makes all request, request parameters, session, and servlet context attributes available to templates through Request, RequestParameters, Session, and Application variables. For example :
    ${Session["user"]}
  • The scope variables are also available via automatic scope discovery. That is, writing Application.attrName, Session.attrName, Request.attrName is not mandatory; it's enough to write attrName, and if no such variable was created in the template, it will search the variable in Request, and then in Session, and finally in Application.
    <#assign ww=JspTaglibs["/WEB-INF/webwork.tld"]>
  • It creates a variable with name JspTaglibs, that can be used to load JSP taglibs. For example:
    <#assign ww=JspTaglibs["/WEB-INF/webwork.tld"]>
    ...
    <@ww.property value="myVar"/>	

     

    Sitemesh context attributes
    baserequest.getContextPath()
    titleParsed page title (<title>...<title>)
    headParsed page head
    bodyParsed page body
    pageSiteMesh's internal Page object
     
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值