先白扯两句,为什么用SiteMesh,当然是减少重复代码,让程序员更加转自己的那一小块。

优点呢:结构化,重用

缺点:呵呵呵,性能,尤其是GC

至于3.0是不是有很大提升,请大神来分析一下。

性能你还可以百度一下,哈吼吼。

1. Maven环境项目搭建,省略一千字

2.POM.xml 依赖配置,我这里采用的是3.0.0,

如果是eclipse + tomcat 会自动下载依赖和build,如果修改了配置可能需要手动update和build

详细情况参考:http://mvnrepository.com/artifact/org.sitemesh/sitemesh

<dependency>
<groupId>org.sitemesh</groupId>
<artifactId>sitemesh</artifactId>
<version>3.0.0</version>
</dependency>

3. 修改/WEB-INF/web.xml,添加

  <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>

具体参考:http://wiki.sitemesh.org/wiki/display/sitemesh3/Getting+Started+with+SiteMesh+3

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 bare 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 bare 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

需要注意的

1. 关于 sitemesh3.xm 的配置

path 是网站访路径
decorator 是本地文件路径

最新文章

  1. Ext JS 如何动态加载JavaScript创建窗体
  2. 百度地图里面搜索到的公司商家电话导出表格?怎样将把百度地图里面搜索到的公司 电话 地址 等数据导出excel里?
  3. 学习 MySQL-DBA常用SQL汇总
  4. 【poj3461】 Oulipo
  5. 2.在程序中如何实现Cookie信息的设置,读取和删除
  6. Linux中的模式转换
  7. QT正则表达式
  8. &lt;精华篇&gt;:iOS视频大全-持续更新
  9. 如何使用W5300实现ADSL连接(一)
  10. asp.net 如何引用dll
  11. MVC3+EF4.1学习系列(九)-----EF4.1其他的一些技巧的使用
  12. bzoj3932
  13. 每天一个Linux命令(23)--linux 目录结构(一)
  14. lgp20151222 java中如何将Object类型转换为int类型
  15. Codechef July Challenge 2018 : Picking Fruit for Chefs
  16. 招聘移动APP、接口、自动化、性能和安全方面的兼职测试讲师
  17. 【zheng环境准备】安装activemq
  18. SQL Server性能优化(6)查询语句建议
  19. Unity3D学习笔记(十四):Animation旧动画
  20. 巡风代码架构简介以及Flask的项目文件结构简介

热门文章

  1. 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误
  2. SQLServer学习笔记系列4
  3. ReflectionHelper
  4. 我读过的最好的epoll讲解--转自”知乎“
  5. C#的变迁史 - C# 2.0篇
  6. jquery简单原则器(匹配偶数元素)
  7. Java 读取Properties文件时应注意的路径问题
  8. mysql中,ENCODE警告---Warning Code : 1287
  9. Linux修改SSH端口,并禁止Root远程登陆
  10. Centos安装Memcached和(Nginx)Memcache扩展详细教程