---
title: "我的sitemash Helloworld"
author: "bepatient"
date: "2010-11-09T21:58:28Z"
tags: []
categories: ["编程语言"]
slug: "sitemash-helloworld"
draft: false
lastmod: "2010-11-09T22:03:30Z"
id: 807029
url: "https://bepatient.iteye.com/blog/807029"
---

首先，导入所需要的包：本例子的包是sitemash-2.4.1.jar。 建立web.xml为sitemash建立相关配置如下：

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<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>
</web-app>
```

在WebRoot文件夹下建立一个新的文件夹decorators，在该文件夹下建立一个文件，此文件即为要在“每一个”页面上显示的文件：

```
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator"
	prefix="decorator"%>
<html>
	<head>
		<title><decorator:title default="装饰器页面..." />hello</title>
		<decorator:title></decorator:title>
	</head>
	<body>
		sitemesh的例子
		<hr>
		<decorator:body />
		<hr>
		http://bepatient.iteye.com
	</body>
</html>
```

 在WEB-INF文件夹下建立decorators.xml页面为以上页面配置：

```
<?xml version="1.0" encoding="utf-8"?>
<decorators defaultdir="/decorators">
	<!-- 此处用来定义不需要过滤的页面 -->
	<excludes>
	</excludes>
	<!-- 用来定义装饰器要过滤的页面 -->
	<decorator name="main" page="main.jsp">
		<pattern>/*</pattern>
	</decorator>
</decorators>  
```

 建立需要加上装饰页面的页面：

```
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8"%>
<html>
	<head>
		<title>本页</title>
	</head>
	<body>
		<p>
			本页只有一句，就是本句.
		</p>
	</body>
</html>
```

 当在浏览器上访问index.jsp时，页面中就会出现main.jsp的内容和index.jsp的内容。