`
thinktothings
  • 浏览: 777615 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Getting started with SiteMesh3

 
阅读更多

http://www.sitemesh.org/

 

 

Getting started with SiteMesh3

Introduction

This tutorial is a quick introduction to using SiteMesh3 in a web-application. It covers:

  • A high level overview of how SiteMesh3 works
  • Installation and configuration
  • Building and applying a simple decorator

It is recommend you read the high level SiteMesh Overview before this tutorial.

SiteMesh in web applications

In a web application, SiteMesh acts as a Servler Filter. It allows requests to be handled by the Servlet engine as normal, but the resulting HTML (referred to as the content ) will be intercepted before being returned to the browser.

The intercepted content has certain properties extracted (typically the contents of the <title> , <head> and <body> tags and is then passed on to a second request that should return the common look and feel for the site (referred to as the decorator ). The decorator contains placeholders for where the properties extracted from the content should be inserted.

Under the hood, a key component of the SiteMesh architecture is the content processor . This is an efficient engine for transforming and extracting content from HTML content. For most users, it's fine to use it as it comes, but it is also possible to define your own transformation and extraction rules.

SiteMesh does not care what technologies are used to generate the content or the decorator. They may be static files, Servlet, JSPs, other filters, MVC frameworks, etc. So long as it's served by the Servlet engine, SiteMesh can work with it.

Dependencies

Running SiteMesh3 requires at least:

  • JDK 1.5
  • A Servlet 2.5 compliant container
  • The SiteMesh runtime library

The SiteMesh library should be downloaded and placed in /WEB-INF/lib/ .

Setup

Insert the SiteMesh Filter in /WEB-INF/web.xml :

<web-app>

  ...
 
  <filter>
    <filter-name>sitemesh
</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter
</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>sitemesh
</filter-name>
    <url-pattern>/*
</url-pattern>
  </filter-mapping>

</web-app>

Deploy the web-application to your Servlet container. From this point onwards, this tutorial assumes the web-app is running at http://myserver/ .

Creating a decorator

The decorator contains the common layout and style that should be applied to the pages in the web application. It is a template that contains place holders for the content's <title> , <head> and <body> elements.

At the bear minimum, it should contain:

<html>
  <head>
    <title><sitemesh:write property='title'/>
</title>
    <sitemesh:write property='head'/>

  </head>
  <body>
    <sitemesh:write property='body'/>

  </body>
</html>

The <sitemesh:write property='...'/> tag will be rewritten by SiteMesh to include properties extracted from the content. There are more properties that can be extracted from the content and it's possible to define your own rules - that will be covered in another tutorial.

The bear minimum decorator isn't very useful. Let's add some style and a bit of common layout.

Create the file /decorator.html in your web-app, containing:

<html>
  <head>
    <title>SiteMesh example: <sitemesh:write property='title'/>
</title>
    <style type='text/css'>
      /* Some CSS */
      body { font-family: arial, sans-serif; background-color: #ffffcc; }
      h1, h2, h3, h4 { text-align: center; background-color: #ccffcc; border-top: 1px solid #66ff66; }
      .mainBody { padding: 10px; border: 1px solid #555555; }
      .disclaimer { text-align: center; border-top: 1px solid #cccccc; margin-top: 40px; color: #666666; font-size: smaller; }
    </style>
    <sitemesh:write property='head'/>

  </head>
  <body>

    <h1 class='title'>SiteMesh example site: <sitemesh:write property='title'/>
</h1>

    <div class='mainBody'>
      <sitemesh:write property='body'/>

    </div>

    <div class='disclaimer'>Site disclaimer. This is an example.</div>

  </body>
</html>

In this example, the decorator is a static .html file, but if you want the decorator to be more dynamic, technologies such as JSP, FreeMarker, etc can be used. SiteMesh doesn't care - it just needs a path that can be served content by the Servlet engine.

Configuration

SiteMesh needs to be configured to know about this decorator and what it should do with it.

The configuration file should be created at /WEB-INF/sitemesh3.xml :

<sitemesh>
  <mapping path="/*
" decorator="/decorator.html
"/>
</sitemesh>

This tells SiteMesh that requests matching the path /* (i.e. all requests) should be decorated with /decorator.html that we just created.

If you don't like the idea of having to use XML to configure SiteMesh, don't worry - there are alternative mechanisms including directly in WEB-INF/web.xml , programatically through a Java API, through Spring, by naming convention, or any custom way you may choose to plug in. These are explained further in another article .

Creating some content

Now to create some content. This is defined in plain HTML content. Create /hello.html :

<html>
  <head>
    <title>Hello World
</title>
    <meta name='description' content='A simple page'>

  </head>
  <body>
    <p>Hello <strong>world</strong>!</p>

  </body>
</html>

Like the decorator, the content may be static files or dynamically generated by the Servlet engine (e.g. JSP).

The result

Pointing your browser to http://myserver/hello.html will serve the content you just created, with the decorator applied. The resulting merged HTML will look like this:

<html>
  <head>
    <title>SiteMesh example: Hello World
</title>
    <style type='text/css'>
      /* Some CSS */
      body { font-family: arial, sans-serif; background-color: #ffffcc; }
      h1, h2, h3, h4 { text-align: center; background-color: #ccffcc; border-top: 1px solid #66ff66; }
      .mainBody { padding: 10px; border: 1px solid #555555; }
      .disclaimer { text-align: center; border-top: 1px solid #cccccc; margin-top: 40px; color: #666666; font-size: smaller; }
    </style>
    <meta name='description' content='A simple page'>

  </head>
  <body>

    <h1 class='title'>SiteMesh example site: Hello World
</h1>

    <div class='mainBody'>
      <p>Hello <strong>world</strong>!</p>

    </div>

    <div class='disclaimer'>Site disclaimer. This is an example.</div>

  </body>
</html>

As you can see, the <title> , <head> and <body> have been extracted from the content and inserted into the decorator template.

Summary

A quick recap:

  • SiteMesh is installed by dropping the library jar in /WEB-INF/lib and creating a filter (with mapping) in /WEB-INF/web.xml
  • It can be configured by creating a /WEB-INF/sitemesh3.xml file, or through other configuration methods
  • The filter intercepts requests to Content , runs it through the Content Processor and merges with a Decorator
  • The Content is defined by an HTML page, that contains the vanilla HTML content of the site
  • The Decorator is also defined by an HTML page, that contains the look and feel of the site, and placeholder <sitemesh:write> tags to indicate where the Content should be merged in
  • The Content Processor contains the rules for extracting and transforming the content - it has some simple default rules and can be customized

 

 

 

 

 

 

 

 

 

 

 

 

 

====================================================================

其实就只有这一点点就可以用了

====================================================================

SiteMesh example site: Hello World

Well hello there, fine world.

And so concludes this SiteMesh example.

How it works

  • This page (/index.html ) contains vanilla HTML content.
  • SiteMesh is configured (in /WEB-INF/web.xml ) to apply a decorator to all paths (/* ).
  • The decorator (/decorator.html ) contains the common look and feel that is applied to the site.
Site disclaimer. This is an example.
分享到:
评论

相关推荐

    sitemesh3-demo

    【sitemesh3-demo】是一个基于Sitemesh3框架的示例项目,旨在展示如何在Web应用程序中有效地实现页面布局和装饰功能。Sitemesh是一个开源的Java Web应用框架,主要用于增强网页的外观和结构,通过定义装饰模板,可以...

    Sitemesh 3 的使用及配置

    除了基本的装饰器配置之外,还可以对`sitemesh3.xml`进行更细致的配置: - **支持多种MIME类型**: ```xml &lt;mime-type&gt;text/html&lt;/mime-type&gt; &lt;mime-type&gt;application/vnd.wap.xhtml+xml &lt;mime-type&gt;application...

    sitemesh3官方下载包

    **Sitemesh3** 是一个开源的网页布局和装饰框架,用于Java Web应用程序。它主要目的是帮助开发者统一网站的外观和感觉,通过提供一种简单的方式来装饰(或模板化)整个Web应用中的页面。Sitemesh3是Sitemesh项目的第...

    SpringMVC-Freemarker_Sitemesh3例子

    **SpringMVC、Freemarker与Sitemesh3详解** SpringMVC是Spring框架的一部分,它是一个用于构建Web应用程序的模型-视图-控制器(MVC)架构。SpringMVC提供了一个灵活的处理机制,包括处理器映射、视图解析、数据绑定...

    sitemesh3包

    **Sitemesh3 SDK详解** Sitemesh 是一个开源的 Web 应用程序框架,用于增强网站的布局和装饰功能。Sitemesh3 是其第三个主要版本,它提供了更现代的架构和更好的性能,旨在帮助开发者更高效地管理和美化他们的网页...

    springmvc + mybatis + sitemesh3 超简洁例子

    springmvc + mybatis + sitemesh3 超简洁例子 整合springmvc mybatis 方法请看: http://blog.csdn.net/kokoyuo/article/details/52808510

    springmvc + mybatis + sitemesh3登陆 超简洁例子mysql

    在本项目中,我们探索的是一个基于Spring MVC、MyBatis和Sitemesh3的超简洁登录示例,其中还集成了MySQL数据库和MD5加密验证。这是一个经典的Java Web开发框架组合,常用于构建高效、可扩展的企业级应用。下面我们将...

    sitemesh教程

    ##### 3. 配置装饰器 - 创建装饰器配置文件`[web-app]/WEB-INF/decorators.xml`。 - 文件结构示例: ```xml &lt;!-- 在这里定义装饰器 --&gt; ``` ##### 4. 可选配置 - 创建可选配置文件`[web-app]/WEB-INF/...

    SiteMesh教程及SiteMesh官方文档翻译

    3. **负载测试**:模拟不同并发用户数量,观察系统的响应时间。 **实验结果** - 在低并发情况下,使用SiteMesh对性能影响不大。 - 随着并发用户数的增加,使用SiteMesh的应用程序响应时间略有提升,但整体仍然稳定...

    sitemesh

    3. **模块化**:易于添加、修改和重用页面组件。 4. **可扩展性**:支持与其他模板引擎集成,适应各种开发需求。 然而,Sitemesh也有其局限性,例如对于JavaScript驱动的单页应用(SPA)支持不够理想,因为它主要是...

    sitemesh技术的应用.doc

    标题:Sitemesh技术的应用 描述与标签:Sitemesh技术的应用 Sitemesh技术是Web开发领域中一种用于页面装饰的重要工具,尤其在Java Web应用程序中被广泛应用。其核心功能在于能够统一网站的外观风格,使不同功能...

    sitemesh3demo附配置说明

    `sitemesh3demo附配置说明`这个主题涉及到的是一个使用Sitemesh3框架的演示项目,其中包含了配置的详细指南。Sitemesh是一个开源的Web应用程序模板引擎和页面装饰框架,它主要用来帮助开发者实现网页布局、样式统一...

    siteMesh demo+文档

    3. **定义装饰模板**:SiteMesh允许你创建一个或多个装饰模板,这些模板定义了页面的结构,如页眉、页脚、侧边栏等。你可以使用HTML或者JSP来编写模板。 4. **应用装饰**:通过在JSP页面中使用特殊的注解(例如`&lt;@...

    java sitemesh 页面框架

    Java Sitemesh是一个开源的页面布局和装饰框架,它的主要目标是帮助开发者统一网站的外观和感觉,提高代码复用性,并简化页面的复杂性。Sitemesh通过将页面分为内容区域和装饰模板来实现这一目标,使得开发者可以...

    SiteMesh

    3. **配置SiteMesh**: 在`sitemesh.xml`中指定布局文件的位置和装饰策略。 4. **标记页面内容**: 在需要装饰的JSP或HTML页面中添加特殊的SiteMesh注释或标签,告知SiteMesh如何插入到布局文件中。 5. **运行应用**...

    sitemesh框架简单例子

    Sitemesh是一款强大的Web页面布局和装饰框架,它主要用于帮助开发者实现网站的统一外观和感觉。这个框架的主要功能是将页面的主体内容与页眉、页脚、侧边栏等通用部分分离,使得在大量网页中保持一致的设计风格变得...

    页面装饰器(sitemesh)实例源代码

    3. **创建装饰模板**:Sitemesh使用HTML文件作为装饰模板,你可以根据需求创建一个基础模板,例如`layout.html`,该模板通常包含页头、页脚、侧边栏等公共部分。 4. **设置页面内容**:对于每个需要装饰的页面,...

    Getting-Started-with-Grails-Chinese

    - **核心组件**:Grails 的核心架构基于三个成熟的 Java 开源框架:**Spring**、**Hibernate** 和 **SiteMesh**。 - **Spring**:用于管理应用程序的依赖注入和服务层逻辑。 - **Hibernate**:提供对象关系映射 ...

    sitemesh-2.2.1.jar sitemesh-2.2.1.jar

    3. 如果需要,Sitemesh会先执行原始JSP或Servlet,获取内容页面的HTML输出。 4. 接着,Sitemesh会将这个内容插入到预先定义好的装饰模板中,这个模板定义了页面的整体结构,如头部、底部、侧边栏等。 5. 最后,...

Global site tag (gtag.js) - Google Analytics