什么是SiteMesh?
SiteMesh是一个轻量级灵活的Java web应用框架,它应用了四人帮(Gang of Four)的装饰模式允许内容和表现有一个清晰的分离
SiteMesh is a lightweight and flexible Java web application framework that applies the Gang of Four decorator pattern to allow a clean separation of content from presentation.
Work with the simple content of your website and have the complex presentation code centrally applied (decorated) just before delivery to the client.
SiteMesh integrates quickly starting with just 4 files.
At the same time, SiteMesh has many advanced features and works with popular frameworks such as Spring and Struts.
Why Use SiteMesh
Write your content once and present it in many different ways,
Introduction
This tutorial teaches you how to set up SiteMesh within a Java Web Development Environment and should take about 5 minutes or less.
Install SiteMesh Manually
Installing SiteMesh into your web application consists of three steps,
Add the SiteMesh library file to WEB-INF/lib
Add the SiteMesh filter to web.xml
Create a blank decorators.xml file
Setup Library File
Copy the SiteMesh jar file into your web application's WEB-INF/lib directory. In this example we downloaded SiteMesh 2.4.1 which uses the jar name, sitemesh-2.4.1.jar.
Add SiteMesh Filter
Add the following to WEB-INF/web.xml within the tag,
sitemesh
com.opensymphony.sitemesh.webapp.SiteMeshFilter
sitemesh
/*
Here is a sample web.xml,
mirabeau
index.html
index.jsp
sitemesh
com.opensymphony.sitemesh.webapp.SiteMeshFilter
sitemesh
/*
Add Decorator Controller File
Create the a decorators.xml file in your WEB-INF directory,
decorators.xml
If you start up your application server (container) and get no errors then SiteMesh is is ready to go. However, SiteMesh will not do anything yet. Continue to Start Using SiteMesh in 10 Minutes.
Introduction
The best approach to understanding SiteMesh is to use it. Assuming that SiteMesh is setup in your web application this tutorial will show you how to master the most powerful aspect of SiteMesh, decorating pages as illustrated below,
The magic happens at step 2 where the pageMenu.jspis rendered as html. Just before the html page is sent down to the client browser, the page is decorated by a single file,basic-theme.jsp.
In this example, a menu is added, a footer is added without any extra code being added toMenu.jsp.
Demo Website
We will start by creating a simple website about a fictional coffee & cake shop called Café Mirabeau.
In your web folder, usually called WebContent, create a folder called data. Inside of data create or download (menu.jsp hours.jsp) these 2 simple html pages.
menu.jsp
/p>
MenuBeverages
Cappucino $3.25
Latte $3.35
Espresso $2.00
Mocha $3.50