SiteMesh2-示例工程
了解SiteMesh的最佳方法是使用它。假设SiteMesh设置在您的Web应用程序中,本教程将展示如何掌握SiteMesh最强大的方面,如下所示装饰页面:

效果发生在第2步,其中Menu.jsp页面呈现为html。在html页面发送到客户端浏览器之前,该页面由单个文件basic-theme.jsp进行装饰。
在此示例中,添加了一个Menu.jsp,添加了一个Footer.jsp,而没有任何额外的代码被添加到Menu.jsp。
示例操作步骤:
0、整体项目结构:

1、POM引入依赖:
<!-- https://mvnrepository.com/artifact/opensymphony/sitemesh -->
<dependency>
<groupId>opensymphony</groupId>
<artifactId>sitemesh</artifactId>
<version>2.4.2</version>
</dependency>
在WEB-INF中新建lib文件夹,并引入SiteMesh标签

2、配置web.xml,增加filter:
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
注意:根据需要进行匹配,这里只匹配jsp页面。
配置标签url:
<taglib>
<taglib-uri>http://www.opensymphony.com/sitemesh/decorator</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-decorator.tld</taglib-location>
</taglib> <taglib>
<taglib-uri>http://www.opensymphony.com/sitemesh/page</taglib-uri>
<taglib-location>/WEB-INF/lib/sitemesh-page.tld</taglib-location>
</taglib>
3、新建decorators文件夹,并新建basic-theme.jsp文件用于做为模板页
<%@ page language="java" contentType="text/html; charset=UTF-8" 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">
<html>
<head>
<title></title>
</head>
<body>
<h1>Header</h1>
<p><b>Navigation</b></p>
<hr />
<decorator:body />
<hr />
<h1><b>Footer</b></h1>
</body>
</html>
4、新建data文件夹,并新建menu.jsp和hours.jsp
menu.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Menu</title>
</head>
<body>
<h1>Beverages</h1>
<p>Cappucino $3.25</p>
<p>Latte $3.35</p>
<p>Espresso $2.00</p>
<p>Mocha $3.50</p>
</body>
</html>
hours.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hours</title>
</head>
<body>
<h1>Weekdays</h1>
<p>5:00pm - 10:00pm</p>
<p>Weekends</p>
<p>5:00pm - 10:00pm</p>
</body>
</html>
5、在WEB-INF文件夹下新建decorators.xml文件,用于页面拦截和排除规则
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/decorators">
<decorator name="basic-theme" page="basic-theme.jsp">
<pattern>/data/*</pattern>
</decorator>
</decorators>
说明:上面的规则拦截data文件夹下的jsp页面,并把内容替换为decorators/basic-theme.jsp模板页的内容。
6、在WEB-INF文件夹下新建sitemesh.xml文件,添加如下规则:
<sitemesh>
<property name="decorators-file" value="/WEB-INF/decorators.xml"/>
<excludes file="${decorators-file}"/> <page-parsers>
<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
</page-parsers>
<decorator-mappers>
<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
<param name="property.1" value="meta.decorator" />
<param name="property.2" value="decorator" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper"/>
<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
<param name="decorator" value="printable" />
<param name="parameter.name" value="printable" />
<param name="parameter.value" value="true" />
</mapper>
<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper"/>
<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
<param name="config" value="${decorators-file}" />
</mapper>
</decorator-mappers>
</sitemesh>
说明:这个文件类似Spring的Bean注入,把来接规则配置好然后通过此文件进行注入和实例化。
7、运行并访问menu.jsp文件,效果如下:

可以看出,内容集成了模板页basic-theme.jsp的内容,其中body部分就是menu.jsp的。
测试工程:https://github.com/easonjim/5_java_example/tree/master/sitemesh/test1
参考:
http://wiki.sitemesh.org/wiki/display/sitemesh/Start+Using+SiteMesh+in+10+Minutes
https://github.com/sitemesh/sitemesh2/blob/master/README.txt
SiteMesh2-示例工程的更多相关文章
- 【AT91SAM3S】英倍特串口示例工程05-UART中,串口是怎样初始化的
在这个示例工程的main.c文件中,进入main之后,没有发现串口功能的任何配置.直接使用了printf这个东西进行输出.将软件下载到开发板上之后,在电脑端使用串口软件,可以看板子有数据发来.说明这个 ...
- Spring示例工程
---------------siwuxie095 创建一个基于 Spring IoC 的小程序的步骤: 建立 Spring 工 ...
- activiti学习2:示例工程activiti-explorer.war的使用
目录 activiti学习2:示例工程activiti-explorer.war的使用 一.搭建开发环境 二.运行示例工程 三.示例工程功能演示 1. 创建流程图 2. 部署流程图 3. 启动流程 4 ...
- Vivado利用IP自带的示例工程和仿真
有时候想查看IP的特性和功能,又不想自己写testbench,Vivado自带的IP示例工程就能派上用场,原来一直不知道怎么打开IP的示例工程 第一步:在原有的工程中新建IP,按照你想要的IP属性,例 ...
- Android Jetpack Compose 引入示例工程
引入 Jetpack Compose 示例工程 去GitHub上找到Compose的示例工程 https://github.com/android/compose-samples ,clone到本地 ...
- live2d+cocos2dx示例工程
环境 : win10 64bit visual studio 2013 cocos2d-x-3.9 Live2D_SDK_OpenGL_2.0.06_2_sample_3.3_en 首先安装visua ...
- xamarin.droid自己的示例工程有些都装不上模拟器,是因为它的architectures选项没设对
也许是版本更迭导致的,有些老工程的architectures不对,如果x86不勾的话,是不能在genymotion的模拟器上跑的.
- node.js express安装及示例网站搭建
1.首先肯定是要安装Node.JS windows cmd依次输入如下命令: cd C:\Program Files\nodejs\ npm install -g expressnpm install ...
- asp.net mvc笔记一,最小的MVC工程
Asp.net MVC项目默认会引用很多第三方插件,特别是现在的5.0,默认示例项目就几十M,搞得都不知道那些才是MVC必须的,是重点,那些是可有可无的. 今天我们就来试验一下,看看一个最小的MVC工 ...
- [Liferay6.2]Liferay入门级portlet开发示例
什么是Portlet 来自百度百科(http://baike.baidu.com/view/58961.htm)的定义如下: portlet是基于java的web组件,处理request并产生动态内容 ...
随机推荐
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...
- [转]ASP .NET MVC 之Entity Framework- code first
本文转自:http://www.cnblogs.com/tomin/archive/2012/02/29/MVC_EntityFramework.html 最近,用到了ASP.NET MVC Ent ...
- Java 8 (5) Stream 流 - 收集数据
在前面已经使用过collect终端操作了,主要是用来把Stream中的所有元素结合成一个List,在本章中,你会发现collect是一个归约操作,就像reduce一样可以接受各种做法作为参数,将流中的 ...
- 关于java日期输出格式
String.format("%tY%tm", new Date(), new Date()): //201905 String.format("%tY-%tm" ...
- 6.12---知道参数的重要性------插入数据-删除数据-修改数据注意Map
---------------
- 微信开发解决if...else..的臃肿
开发中难以避免if...else (switch case ),大量的if...else 让代码可读性低...难以维护 无论是接手别人的代码还是自己写的代码,因为开发周期短可能就往往忽略了这一点. 久 ...
- 下载github项目
两种方法:通过https或者ssh地址 找一个放置项目的文件夹,右键git bash here 输入 $ git clone https://项目地址 通过https 项目地址可以直接复制网页地址,或 ...
- c++中的类型转换--reinterpret_cast
原文链接: 浅析c++中的类型转换--reinterpret_cast转换 reinterpret_cast作用为: 允许将任何指针转换为任何其他指针类型. 也允许将任何整数类型转换为任何指针类型以 ...
- 场景分割:MIT Scene Parsing 与DilatedNet 扩展卷积网络
MIT Scene Parsing Benchmark简介 Scene parsing is to segment and parse an image into different image re ...
- ansible 批量推送ssh秘钥
ansible 批量推送ssh秘钥 参考:http://docs.ansible.com/ansible/authorized_key_module.html # vi /etc/ansible/ho ...