了解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-示例工程的更多相关文章

  1. 【AT91SAM3S】英倍特串口示例工程05-UART中,串口是怎样初始化的

    在这个示例工程的main.c文件中,进入main之后,没有发现串口功能的任何配置.直接使用了printf这个东西进行输出.将软件下载到开发板上之后,在电脑端使用串口软件,可以看板子有数据发来.说明这个 ...

  2. Spring示例工程

    ---------------siwuxie095                                 创建一个基于 Spring IoC 的小程序的步骤:     建立 Spring 工 ...

  3. activiti学习2:示例工程activiti-explorer.war的使用

    目录 activiti学习2:示例工程activiti-explorer.war的使用 一.搭建开发环境 二.运行示例工程 三.示例工程功能演示 1. 创建流程图 2. 部署流程图 3. 启动流程 4 ...

  4. Vivado利用IP自带的示例工程和仿真

    有时候想查看IP的特性和功能,又不想自己写testbench,Vivado自带的IP示例工程就能派上用场,原来一直不知道怎么打开IP的示例工程 第一步:在原有的工程中新建IP,按照你想要的IP属性,例 ...

  5. Android Jetpack Compose 引入示例工程

    引入 Jetpack Compose 示例工程 去GitHub上找到Compose的示例工程 https://github.com/android/compose-samples ,clone到本地 ...

  6. live2d+cocos2dx示例工程

    环境 : win10 64bit visual studio 2013 cocos2d-x-3.9 Live2D_SDK_OpenGL_2.0.06_2_sample_3.3_en 首先安装visua ...

  7. xamarin.droid自己的示例工程有些都装不上模拟器,是因为它的architectures选项没设对

    也许是版本更迭导致的,有些老工程的architectures不对,如果x86不勾的话,是不能在genymotion的模拟器上跑的.

  8. node.js express安装及示例网站搭建

    1.首先肯定是要安装Node.JS windows cmd依次输入如下命令: cd C:\Program Files\nodejs\ npm install -g expressnpm install ...

  9. asp.net mvc笔记一,最小的MVC工程

    Asp.net MVC项目默认会引用很多第三方插件,特别是现在的5.0,默认示例项目就几十M,搞得都不知道那些才是MVC必须的,是重点,那些是可有可无的. 今天我们就来试验一下,看看一个最小的MVC工 ...

  10. [Liferay6.2]Liferay入门级portlet开发示例

    什么是Portlet 来自百度百科(http://baike.baidu.com/view/58961.htm)的定义如下: portlet是基于java的web组件,处理request并产生动态内容 ...

随机推荐

  1. C#的装箱与拆箱与基本类型

    装箱:值类型转换为对象类型, 实例: int val = 8; object obj = val;//整型数据转换为了对象类型(装箱) 拆箱:之前由值类型转换而来的对象类型再转回值类型, 实例: in ...

  2. ora-20000 unable to analyze

    ora-20000 unable to analyze 无法分析表 check: select * from wmsprdata.cmp3$88278表不存在. result:应该是系统自动任务2:0 ...

  3. 385 Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it.Each e ...

  4. arduino 字符解析

    Arduino String.h库函数详解   此库中包含1 charAT()2 compareTo()3 concat()4 endsWith()5 equals()6 equalslgnoreCa ...

  5. 百度AI车牌识别测试

    测试背景 百度已发布诸多AI应用,其中包含车牌识别,免费使用量是200次/日.付费的话,按月调用次数在20万次到50万次之间,每日10000次,月费用为0.0035*300000=1050元. 详见: ...

  6. ElasticSearch 安装使用

    安装: 1.下载ElasticSearch.解压到相关文件夹 2.运行elasticsearch.bat,启动程序 3.在浏览器输入:http://localhost:9200/,显示相关Es内容即安 ...

  7. org.apache.jasper.JasperException: javax.el.PropertyNotFoundException: Property [xxx] not readable on type [xxx]

    由于javaBean中的属性是custFullName,所以在使用jsp的时候,通过el表达式获取属性的值<td>${m.CustFullName}</td>.但是加载页面的时 ...

  8. sql IIF函数的应用

    工作中由于用各种框架,业务也不是很复杂,一直没怎么写过复杂的sql,今天写了一个 数据如下 代码如下 Sub 班级分数等级分析(shname) Dim Con As Object, rs As Obj ...

  9. 牛客多校Round 2

    Solved:3 rank:187 H.travel 题意:给一颗带有点权的树 找三条不相交的链 使得点权最大 题解:使用树形DP dp[x][i][0/1] 表示x节点选择i条链 有没有经过x的链 ...

  10. PHP下载压缩包文件

    PHP 压缩文件需要用到 ZipArchive 类,Windows 环境需要打开 php_zip.dll扩展. 压缩文件 $zip = new ZipArchive(); // 打开一个zip文档,Z ...