Introduction to sitemesh, sitemesh

Source: Internet
Author: User

Introduction to sitemesh, sitemesh

1 Overview

SiteMesh is a framework based on Web page layout, decoration, and integration with existing Web applications. It helps us create consistent page layout and appearance in projects composed of a large number of pages, such as consistent navigation bar, consistent banner, consistent copyright, and so on. It can not only process dynamic content, such as jsp, php, asp, but also static content, such as htm content, so that its content meets the requirements of your page structure. It can even embed an HTML file into another file as a panel like include. All of these are the most vivid implementations of the GOF Decorator mode. Although it is implemented by the java language, it can be well integrated with other Web applications.

2 Principle

The SiteMesh framework is an excellent page decoration framework developed by the OpenSymphony team. It filters user requests and filters responses from servers to clients, add some decoration (header, footer, etc.) to the original page and return the result to the client. Through SiteMesh page decoration, You can provide better code reuse. All page decoration effects are coupled to the target page without using the include command to include decoration effects, the target page is completely separated from the decoration page. If all pages use the same decoration device, the whole Web application has a uniform style.

3 demo

  • Place the sitemesh-2.3.jar under the [web-app]/WEB-INF/lib directory
  • In the [web-app]/WEB-INF/create a new decorators. xml file that contains the following:
<? Xml version = "1.0" encoding = "UTF-8"?> <Decorators defaultdir = "/decorators"> <! -- This is used to define pages that do not need to be filtered --> <excludes> </excludes> <! -- Defines the page to be filtered by the decorator --> <decorator name = "main" page = "main. jsp "> <pattern>/* </pattern> </decorator> </decorators>

Add the following content in [web-app]/WEB-INF/web. xml:

<filter>  <filter-name>sitemesh</filter-name>  <filter-class>  com.opensymphony.module.sitemesh.filter.PageFilter   </filter-class>  </filter>    <filter-mapping>  <filter-name>sitemesh</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>

Create a decorators folder under [web-app] and create a decoration page main. jsp under this file, which contains the following content:

<% @ Page language = "java" contentType = "text/html" pageEncoding = "UTF-8" %> <% @ taglib uri = "http://www.opensymphony.com/sitemesh/decorator" prefix = "decorator" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Create the decorated page hours. jsp under [web-app], which contains the following content:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

4. Summary
From the above example, we can see that through SiteMesh decoration, you do not need to include headers, footer, and other common files in each target page. The decoration (target) page and the decoration page are completely separated. This article only gives a brief introduction to SiteMesh. SiteMesh can be used in combination with open-source template tools such as Velocity and FreeMarker to reduce page development complexity.

 

 

Reference: http://www.iteye.com/topic/251521.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.